UITabBarController的简单使用
注意事项:特殊高度,UITabBar的高度是49,UINavigationBar的高度是44
UITabBar的内部结构
右边红色的view覆盖在左边的蓝色view上,最终效果图是图2
图1:
图2:
1 UITabBarController的使用步骤
初始化UITabBarController
设置UIWindow的rootViewController为UITabBarController
根据具体情况,通过addChildViewController方法添加对应个数的子控制器
2 UITabBarController的子控制器
2.1 UITabBarController添加控制器的方式有2种
添加单个子控制器
- (void)addChildViewController:(UIViewController *)childController;
设置子控制器数组
@property(nonatomic,copy) NSArray *viewControllers;
3 UITabBar
如果UITabBarController有N个子控制器,那么UITabBar内部就会有N个UITabBarButton作为子控件
如果UITabBarController有4个子控制器,那么UITabBar的结构大致如下图所示
![](http://images0.cnblogs.com/blog/554901/201412/042150432015797.png)
4 UITabBarButton
![](http://images0.cnblogs.com/blog/554901/201412/042154543113081.png)
5 App主流UI框架结构
![](http://images0.cnblogs.com/blog/554901/201412/042156007649645.png)
6UITabBar的原理
程序启动后如果是VC1显示在图1上,当点击UITabBar内部的按钮切换到VC2时
会把VC1移到一边去(注意不是直接把VC2覆盖到VC1上面,如果直接覆盖性能会很差),
然后把VC2放到图1上,同理如果点击UITabBar内部的按钮切换到VC3,这个时候会把VC2移到一边去,
然后把VC3显示到图1上,整个切换过程VC1,VC2,VC3都不会被销毁(除非发生了内存警告,才会销毁不在显示的VC)。
图1
![](http://images0.cnblogs.com/blog/554901/201412/042255564058558.png)
图2
![](http://images0.cnblogs.com/blog/554901/201412/042256562647270.png)
![](http://images0.cnblogs.com/blog/554901/201412/042257228427450.png)
![](http://images0.cnblogs.com/blog/554901/201412/042257391551368.png)
7 iOS6中控制器的高度=整个屏幕的高度-状态栏的高度(20)-UITabBar的高度(49)
iOS7中控制器的高度=整个屏幕的高度
8 代码示例
// 添加三个控制器到UITabBarController上
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UITabBarController *tabbarVc = [[UITabBarController alloc]init];
self.window.rootViewController = tabbarVc;
UIViewController *vc1 = [[UIViewController alloc]init];
vc1.view.backgroundColor = [UIColor redColor];
// [tab addChildViewController:vc1];
vc1.tabBarItem.title = @"空间";
vc1.tabBarItem.image = [UIImage imageNamed:@"tab_qworld_nor"];
UIViewController *vc2 = [[UIViewController alloc]init];
vc2.view.backgroundColor = [UIColor blueColor];
vc2.tabBarItem.title = @"设置";
vc2.tabBarItem.image = [UIImage imageNamed:@"tab_me_nor"];
// [tab addChildViewController:vc2];
UIViewController *vc3 = [[UIViewController alloc]init];
vc3.view.backgroundColor = [UIColor orangeColor];
vc3.tabBarItem.title = @"我";
vc3.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"];
tabbarVc.viewControllers = @[vc1,vc2,vc3];
[self.window makeKeyAndVisible];
return YES;
}
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UITabBarController *tabbarVc = [[UITabBarController alloc]init];
self.window.rootViewController = tabbarVc;
UIViewController *vc1 = [[UIViewController alloc]init];
vc1.view.backgroundColor = [UIColor redColor];
// [tab addChildViewController:vc1];
vc1.tabBarItem.title = @"空间";
vc1.tabBarItem.image = [UIImage imageNamed:@"tab_qworld_nor"];
UIViewController *vc2 = [[UIViewController alloc]init];
vc2.view.backgroundColor = [UIColor blueColor];
vc2.tabBarItem.title = @"设置";
vc2.tabBarItem.image = [UIImage imageNamed:@"tab_me_nor"];
// [tab addChildViewController:vc2];
UIViewController *vc3 = [[UIViewController alloc]init];
vc3.view.backgroundColor = [UIColor orangeColor];
vc3.tabBarItem.title = @"我";
vc3.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"];
tabbarVc.viewControllers = @[vc1,vc2,vc3];
[self.window makeKeyAndVisible];
return YES;
}