【iOS进阶】【多控制器】UITabBarController控制器
一、UITabBarController控制器的简单使用
1> UITabBarController
跟UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是QQ、微信等应用
2> UITabBarController的简单使用
1.UITabBarController的使用步骤:
1.初始化UITabBarController;
2.设置UIWindow的 rootViewController为UITabBarController;
3.根据具体情况,通过addChildViewController方法添加 对应个数的子控制器。
2.具体实现:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; // 1.创建tabbar控制器 UITabBarController *tabbarVc = [[UITabBarController alloc] init]; // 2.设置为window的根控制器 self.window.rootViewController = tabbarVc; // 3.添加子控制器 UIViewController *vc1 = [[UIViewController alloc] init]; vc1.view.backgroundColor = [UIColor redColor]; vc1.tabBarItem.title = @"联系人"; vc1.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"]; // [tabbarVc addChildViewController:vc1]; UIViewController *vc2 = [[UIViewController alloc] init]; vc2.view.backgroundColor = [UIColor blueColor]; vc2.tabBarItem.title = @"动态"; vc2.tabBarItem.image = [UIImage imageNamed:@"tab_qworld_nor"]; // [tabbarVc addChildViewController:vc2]; UIViewController *vc3 = [[UIViewController alloc] init]; vc3.view.backgroundColor = [UIColor greenColor]; vc3.tabBarItem.title = @"设置"; vc3.tabBarItem.image = [UIImage imageNamed:@"tab_me_nor"]; // [tabbarVc addChildViewController:vc3]; //UITabBarController添加控制器的方式有2种 //添加单个子控制器 - (void)addChildViewController:(UIViewController *)childController; //设置子控制器数组 @property(nonatomic,copy) NSArray *viewControllers;
tabbarVc.viewControllers = @[vc1, vc2, vc3]; [self.window makeKeyAndVisible]; return YES; }
3.UITabBarController的UITabBar属性说明:
4.UITabBarController中UITabBar的UITabBarButton属性说明:
一、Storyboard上实现UITabBarController控制器
1> 删除Storyboard自带的控制器,添加一个UITabBarController控制器。它是长这样的:
2> 设置Storyboard的main控制器属性,它是这样设置的:
1.点击UITabBarController上的控制器按钮来到属性设置界面,ls lnitial View Controller属性打勾即可。
3>删除UITabBarController自带的子控制器,添加两个View Controller控制器
右击UITabBarController,选择view controllers拖线至View Controller控制器。那么该View Controller就是它的子控制器了。
view controllers是数组,即UITabBarController可以有很多子控制器
4>在此设置各个子控件在TabBar上显示的属性

浙公网安备 33010602011771号