开头
UICollectionViewController
实际用处
基础用法
- (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;
}
- (__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]];
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];
}
总结
- 对于
UITabBarController来说,最重要的就是子控制器的添加,在实际开发中,往往是UITabBarController嵌套UINavigationController,在嵌套实际显示内容的控制器