- (instancetype)init UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new]; //item大小 layout.itemSize = [UIScreen mainScreen].bounds.size; //上下间距,在水平方向滚动时,变为左右间距 layout.minimumLineSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; return [super initWithCollectionViewLayout:layout];
  • 注册单元格等相关设置,单元格重用符oc中一般使用静态变量修饰static NSString * const reuseIdentifier = @"guide_cell";,swift直接let
  • - (void)viewDidLoad {
        [super viewDidLoad];
        //注册单元格
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier: reuseIdentifier];
        //设置分页
        self.collectionView.pagingEnabled = YES;
        //取消滚动条
        self.collectionView.showsHorizontalScrollIndicator = NO;
        //取消弹性效果
        self.collectionView.bounces = NO;
    
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return 1;
    
  • 设置单元数
  • - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return 4;
    
  • 返回cell样式,下面的颜色是随机颜色
  • - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
        return cell;
    
  • 实际开发中,cell是自定义设置,可以利用自定义cell中的图片名属性和图片名,结合indexPath进行每个单元格不同界面的显示
  • NSString *imageName = [NSString stringWithFormat:@"guide%zdBackground", indexPath.row + 1];
    UIImage *image = [UIImage imageNamed:imageName];
    cell.image = image;
    
  • 有时候会需要在滚动结束的得时候进行动画或者显示特别的控件,在下面这个函数进行处理
  • - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    

    UITabBarController

  • 实现类似微信、微博的界面切换的功能
  • 类似抽屉的概念,分别将界面从左至右依次添加到抽屉里
  • 设置title,如果是先放在UINavigationController,再添加到UITabBarController,设置title也会影响两个地方标题
  • RedViewController *redVC = [RedViewController new];
    GreenViewController *greenVC = [GreenViewController new];
    GrayViewController *grayVC = [GrayViewController new];
    YellowViewController *yellowVC = [YellowViewController new];
    BlueViewController *blueVC = [BlueViewController new];
    //设置bar的图片
    redVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"];
    //设置bar的标题
    redVC.title = @"red";
    greenVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_profile"];
    greenVC.title = @"green";
    grayVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"];
    grayVC.title = @"gray";
    yellowVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_message_center"];
    yellowVC.title = @"yellow";
    blueVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_compose_icon_add"];
    blueVC.title = @"blue";
    [self addChildViewController:redVC];
    [self addChildViewController:greenVC];
    [self addChildViewController:grayVC];
    [self addChildViewController:yellowVC];
    [self addChildViewController:blueVC];
    
    //设置渲染颜色
    [UITabBar appearance].tintColor = [UIColor orangeColor];
    //设置背景图片
    [UITabBar appearance].backgroundImage = [UIImage imageNamed:@"tabbar_background"];
    

    TabBar中间添加按钮

    //创建按钮
    UIButton *btn = [UIButton new];
    [btn setImage:[UIImage imageNamed:@"tabbar_message_center"] forState:UIControlStateNormal];
    //在想要放按钮的位置,增加一个新界面,腾出空间,一般在中间添加
    [self addChildViewController:[UIViewController new]];
    //计算frame,向左边加一段距离
    CGFloat x = 2 * self.tabBar.bounds.size.width / self.viewControllers.count;
    btn.frame = CGRectInset(self.tabBar.bounds, x, 0);
    [self.tabBar addSubview:btn];
    
  • 这里还有个问题,因为是在viewDidLoad中创建的控件,而创建出来的UIButton默认是放在TabBar下方的,所以不能实现按钮的效果,解决方法是,在viewWillAppear时将UIButton带到TabBar上面
  • - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [self.tabBar addSubview:self.btn];
    
  • 视图生命周期相关的整理可以看这里modal跳转与视图生命周期
  • 对于UITabBarController来说,最重要的就是子控制器的添加,在实际开发中,往往是UITabBarController嵌套UINavigationController,在嵌套实际显示内容的控制器
  • 分类:
    iOS
    标签: