At the beginning

  • This is a sub-chapter. For the whole article, see the Learning Diary of Zero-based iOS development

UICollectionViewController

The actual use

  • New Feature interface

Basic usage

  • The basic usage here is directly implementing the basic new feature scroll interface,UICollectionViewYou can look at thisUICollectionView
  • Override the initialization method
- (instancetype)init { UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new]; // Item size layout.itemSize = [UIScreen mainScreen].bounds.size; / / spacing, horizontal scrolling, into left and right spacing layout. MinimumLineSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; return [super initWithCollectionViewLayout:layout]; }Copy the code
  • Register cell and other related Settings. Cell reuse symbol OC is usually decorated with static variablesstatic NSString * const reuseIdentifier = @"guide_cell";Swift direct let
- (void)viewDidLoad { [super viewDidLoad]; / / register cell [self collectionView registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier: reuseIdentifier]; / / set the paging self. CollectionView. PagingEnabled = YES; / / cancel the scrollbar self. CollectionView. ShowsHorizontalScrollIndicator = NO; / / cancel the elastic effect of the self. CollectionView. Bounces = NO. }Copy the code
  • Set the group number
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
Copy the code
  • Set the number of units
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}
Copy the code
  • Return cell style with random colors below
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath  *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 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; }Copy the code

Pay attention to

  • In actual development, the cell is a custom setting. You can use the image name attribute in the custom cell to combine with the image nameindexPathDisplay the different interface of each cell
NSString *imageName = [NSString stringWithFormat:@"guide%zdBackground", indexPath.row + 1];
UIImage *image = [UIImage imageNamed:imageName];
cell.image = image;
Copy the code
  • Sometimes it is necessary to animate or display special controls at the end of the scroll, which is handled in the following function
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
}
Copy the code

UITabBarController

The actual use

  • Realize the interface switch function similar to wechat and Weibo

Basic usage

  • Similar to the concept of a drawer, the interface is added to the drawer from left to right
  • Set title, if firstUINavigationControllerAnd then add toUITabBarControllerTo set uptitleIt also affects both local headings
RedViewController *redVC = [RedViewController new]; GreenViewController *greenVC = [GreenViewController new]; GrayViewController *grayVC = [GrayViewController new]; YellowViewController *yellowVC = [YellowViewController new]; BlueViewController *blueVC = [BlueViewController new]; // Set the bar image redvc.tabbarItem. image = [UIImage imageNamed:@" tabbar_HOME "]; // Set bar title 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];Copy the code

Some of the Settings

// Set the render color [UITabBar appearance]. TintColor = [UIColor orangeColor]; [UITabBar appearance]. BackgroundImage = [UIImage imageNamed:@"tabbar_background"];Copy the code

Add a button in the middle of TabBar

UIButton * BTN = [UIButton new]; [btn setImage:[UIImage imageNamed:@"tabbar_message_center"] forState:UIControlStateNormal]; [self addChildViewController:[UIViewController new]]]; / / frame, to the left to add a distance CGFloat x = 2 * self. TabBar. Bounds. The size, width/self viewControllers. Count; btn.frame = CGRectInset(self.tabBar.bounds, x, 0); [self.tabBar addSubview:btn];Copy the code
  • There’s a problem here, because it’s inviewDidLoadControl created in theUIButtonThe default is to putTabBarThe bottom, so can’t achieve the effect of the button, the solution is, inviewWillAppearWhen willUIButtontoTabBarThe above
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBar addSubview:self.btn];
}
Copy the code
  • For collation of view life cycles, see modal jumps and view life cycles here

conclusion

  • forUITabBarControllerFor example, the most important thing is the addition of child controllers, which is often the case in real developmentUITabBarControllernestedUINavigationControllerIn the nested controller that actually displays the content