IOS development-UITabBarController empty project(61)

I. Overview

Similar to UINavigationController, UITabBarController can also easily manage multiple controllers and easily switch between controllers. Typical examples are applications such as QQ and WeChat.

Two UITabBarController is simple and practical

2.1 Steps to use UITabBarController

  • Initialize UITabBarController

  • Set the rootViewController of UIWindow to UITabBarController

  • According to the specific situation, add the corresponding number of child controllers through the addChildViewController method

    2.2 UITabBar

  • If UITabBarController has N sub-controllers, then there will be N UITabBarButtons as sub-controls inside UITabBar

  • If UITabBarController has 4 sub-controllers, then the structure of UITabBar is roughly as follows

    2.3 UITabBarButton

  • What content is displayed in UITabBarButton is determined by the tabBarItem property of the corresponding sub-controller

  • UITabBarItem has the following properties that affect the content of UITabBarButton:

    • Title text: @property(nonatomic,copy) NSString *title;
    • 图标:@property(nonatomic,retain) UIImage *image;
    • Icon when selected: @property(nonatomic,retain) UIImage *selectedImage;
    • Reminder number: @property(nonatomic,copy) NSString *badgeValue;

2.4 Where to initialize UITabBarController

initialization

  • The didFinishLaunchingWithOptions method in AppDelegate.m was initialized before
  • After Xcode11, initialize in SceneDelegate.m

the reason

  • Xcode 11 new projects will create applications that manage multiple UIWindow through UIScene by default. In addition to AppDelegate, there will also be a SceneDelegate in the project. This is to achieve the result of iPadOS supporting multiple windows
  • AppDelegate.h no longer has a window property, the window property is defined in SceneDelegate.h, AppDelegate has a new proxy method about scene, and SceneDelegate also has a corresponding proxy method

Three codes (SceneDelegate.m—>willConnectToSession method)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    UITabBarController *tb=[[UITabBarController alloc]init]; 
    //Set the controller as the root controller of Window 
    self.window.rootViewController=tb; 
    
    //b. Create a child controller 
    UIViewController *c1=[[UIViewController alloc]init] ; 
    c1.view.backgroundColor=[UIColor grayColor]; 
    c1.view.backgroundColor=[UIColor greenColor]; 
    c1.tabBarItem.title=@"Message"; 
    c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"]; 
    c1.tabBarItem.selectedImage=[UIImage imageNamed:@"tab_recent_select"]; 
    c1.tabBarItem.badgeValue=@"123"; 
    
    UIViewController *c2=[[UIViewController alloc]init]; 
    c2.view.backgroundColor=[UIColor brownColor]; 
    c2.tabBarItem.title=@"Contact";
    c2.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"];
    c2.tabBarItem.selectedImage=[UIImage imageNamed:@"tab_buddy_select"];
    
    UIViewController *c3=[[UIViewController alloc]init];
    c3.view.backgroundColor=[UIColor greenColor];
    c3.tabBarItem.title=@"动态";
    c3.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"];
    c3.tabBarItem.selectedImage=[UIImage imageNamed:@"tab_qworld_select"];
    
    UIViewController *c4=[[UIViewController alloc]init];
    c4.view.backgroundColor=[UIColor blueColor];
    c4.tabBarItem.title=@"设置";
    c4.tabBarItem.image=[UIImage imageNamed:@"tab_me_nor"];
    c4.tabBarItem.selectedImage=[UIImage imageNamed:@"tab_me_select"];
   
    
    //c. Add child controllers to ITabBarController 
    //c.1 the first way 
// [tb addChildViewController:c1];
//    [tb addChildViewController:c2];
    
    //c.2 The second way 
    tb.viewControllers=@[c1,c2,c3,c4];

Four renderings

Five references

Guess you like

Origin blog.csdn.net/Calvin_zhou/article/details/109173949