UICollectionViewController 版本新特性

AppDelegate.m 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    NSString *curVersion =  [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    NSString *lastVersion =  [[NSUserDefaults standardUserDefaults]objectForKey:@"vision"];
    
    NSLog(@"%@",[NSUserDefaults standardUserDefaults]);
    
    UIViewController *rootVC;
    
    
    if ([curVersion isEqualToString:lastVersion]) {
        rootVC = [[TabBarController alloc]init];
    }else{
        rootVC = [[ZCCollectionViewController alloc]init];
        [[NSUserDefaults standardUserDefaults]setObject:curVersion forKey:@"vision"];
    }
    
    self.window.rootViewController =rootVC;
    
    [self.window makeKeyAndVisible];
    return YES;
}

UICollectionViewController

//
//  ZCCollectionViewController.m
//  UICollectionDemo
//
//  Created by iOS on 13/9/30.
//  Copyright © 2015年 iOS. All rights reserved.
//

#import "ZCCollectionViewController.h"
#import "ZCCollectionViewCell.h"
#import "UIView+Frame.h"
@interface ZCCollectionViewController ()
@property(nonatomic,weak)UIPageControl *pageControl;
@end

@implementation ZCCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 设置cell的尺寸
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
    
    [self.collectionView registerClass:[ZCCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self setPageControl];
}

- (void)setUp{
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
}

- (void)setPageControl{
    UIPageControl *control = [[UIPageControl alloc]init];
    control.numberOfPages = 4;
    control.pageIndicatorTintColor = [UIColor blackColor];
    control.currentPageIndicatorTintColor = [UIColor redColor];
    control.center = CGPointMake(self.view.centerX, self.view.centerY);
    [self.view addSubview:control];
    _pageControl = control;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = scrollView.contentOffset.x/scrollView.bounds.size.width+0.5;
    _pageControl.currentPage =page;
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZCCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
//     CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    
    NSString *imageName = [NSString stringWithFormat:@"new_feature_%ld",indexPath.row + 1];
//    if (screenH > 480) { // 5 , 6 , 6 plus
//        imageName = [NSString stringWithFormat:@"new_feature_%ld-568h",indexPath.row + 1];
//    }
    cell.image = [UIImage imageNamed:imageName];
    
    [cell setIndexPath:indexPath count:4];
    
    return cell;
}


@end

UICollectionViewCell.h

//
//  ZCCollectionViewCell.h
//  UICollectionDemo
//
//  Created by iOS on 13/9/30.
//  Copyright © 2015年 iOS. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ZCCollectionViewCell : UICollectionViewCell
@property(nonatomic,weak)UIImage *image;

- (void)setIndexPath:(NSIndexPath *)indexPath count:(int)count;
@end
UICollectionViewCell.m
//
//  ZCCollectionViewCell.m
//  UICollectionDemo
//
//  Created by iOS on 13/9/30.
//  Copyright © 2015年 iOS. All rights reserved.
//

#import "ZCCollectionViewCell.h"
#import "UIView+Frame.h"
#import "TabBarController.h"
@interface ZCCollectionViewCell()
@property (nonatomic, weak) UIImageView *imageView;

@property (nonatomic, weak) UIButton *shareButton;

@property (nonatomic, weak) UIButton *startButton;
@end

@implementation ZCCollectionViewCell


- (UIButton *)shareButton{
    if (_shareButton==nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"分享给大家" forState:UIControlStateNormal];
        [btn setTintColor:[UIColor orangeColor]];
        [btn sizeToFit];
        
        [self.contentView addSubview:btn];
        
        _shareButton = btn;
    }
    return _shareButton;
}

- (UIButton *)startButton{
    if (_startButton==nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"开始" forState:UIControlStateNormal];
        [btn setTintColor:[UIColor orangeColor]];
        [btn sizeToFit];
        [btn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btn];
        _startButton =btn;
    }
    return _startButton;
}

- (UIImageView *)imageView{
    if (_imageView==nil) {
        UIImageView *imageV = [[UIImageView alloc]init];
        _imageView = imageV;
        [self.contentView addSubview:imageV];
    }
    return _imageView;
}

- (void)setImage:(UIImage *)image{
    _image = image;
    self.imageView.image = image;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.imageView.frame =self.bounds;
    
    self.shareButton.center = CGPointMake(self.width*0.5, self.height*0.8);
    self.startButton.center = CGPointMake(self.width*0.5, self.height*0.9);
}

- (void)setIndexPath:(NSIndexPath *)indexPath count:(int)count{
    if (indexPath.row==count-1) {
        self.shareButton.hidden=NO;
        self.startButton.hidden=NO;
    }else{
        self.shareButton.hidden=YES;
        self.startButton.hidden= YES;
    }
}

- (void)start{
    TabBarController *tab = [[TabBarController alloc]init];
    
    
    UIWindow *kwindow =  [UIApplication sharedApplication].keyWindow;
    
    kwindow.rootViewController = tab;
}

@end



要设置 `UICollectionViewController` 的高度,你可以通过以下几种方法之一: 1. 使用自动布局约束:将 `UICollectionViewController` 添加到一个容器视图中,并使用自动布局约束来设置容器视图的高度。这样,`UICollectionViewController` 的高度将会自动根据容器视图的约束而确定。 2. 手动计算高度:如果你希望手动计算 `UICollectionViewController` 的高度,可以在它的父视图控制器中重写 `viewDidLayoutSubviews` 方法,并在该方法中计算并设置 `UICollectionViewController` 的高度。 ```swift override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() // 计算内容的高度 let contentHeight = // 根据你的需求计算内容高度 // 设置 UICollectionViewController 的高度 collectionView.frame = CGRect(x: 0, y: 0, width: collectionView.frame.width, height: contentHeight) } ``` 3. 使用自定义布局:如果你使用自定义的 `UICollectionViewLayout` 来布局 `UICollectionViewController`,你可以在布局类中重写 `collectionViewContentSize` 方法来返回正确的内容尺寸。这样,`UICollectionViewController` 的高度将会根据布局类返回的内容尺寸来确定。 ```swift class CustomLayout: UICollectionViewLayout { // ... override var collectionViewContentSize: CGSize { // 根据你的需求计算内容尺寸 let contentSize = // 计算内容尺寸的逻辑 return contentSize } // ... } ``` 以上是几种常见的设置 `UICollectionViewController` 高度的方法,你可以根据你的具体需求选择适合的方法来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值