特性
* UITabBarController上的视图是平行关系,他们之间不能做页面跳转
* UITabBarController上的页面都是同时加载出来的
* 页面数大于五 自动生成more
实例化
UITabBarController *tabBarController = [[UITabBarController alloc]init];
self.window.rootViewController = tabBarController;
标题
只有视图控制器设置标题时 .title与 .tabBarItem.title无区别
插入导航控制器时:
1. view.title 同时给tabBar与navigationBar设置标题
2. view.navigationItem.title 只给导航栏设置标题
3. view.tabBarItem.title 无效
4. nav.title 只给tabBar设标题
5. nav.tabBarItem.title 只给tabBar设标题
6. nav.navigationItem.title 无效
元素图片设置
//设置图片
view.tabBarItem.image = <#(UIImage *)#>;
nav.tabBarItem.image = <#(UIImage *)#>;
//设置图片 标题
UITabBarItem *tab = [[UITabBarItem alloc]initWithTitle:<#(NSString *)#> image:<#(UIImage *)#> tag:<#(NSInteger)#>];
nav.tabBarItem = tab;
//系统自带图片 标题
UITabBarItem *tab = [[UITabBarItem alloc]initWithTabBarSystemItem:<#(UITabBarSystemItem)#> tag:<#(NSInteger)#>];
view.tabBarItem = tab;
//设置选中前及选中后图片
//iOS7之前的写法
[nav.tabBarItem setFinishedSelectedImage:<#(UIImage *)#> withFinishedUnselectedImage:<#(UIImage *)#>];
//iOS7之后
UITabBarItem *tab = [[UITabBarItem alloc]initWithTitle:<#(NSString *)#> image:<#(UIImage *)#> selectedImage:<#(UIImage *)#>];
view.tabBarItem = tab;
设置徽标
nav.tabBarItem.badgeValue = <#(NSString *)#>;
图文渲染 (让图片保持原样显示)
//选中前
view.tabBarItem.image = [<#(UIImage *)#> imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//选中后
view.tabBarItem.selectedImage = [<#(UIImage *)#> imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
文字渲染
//选中前
NSDictionary *dic1 = @{NSForegroundColorAttributeName:<#(UIColor *)#>,NSFontAttributeName : [UIFont systemFontOfSize:11.0]};
[view.tabBarItem setTitleTextAttributes:dic1 forState:UIControlStateNormal];
//选中后
NSDictionary *dic2 = @{NSForegroundColorAttributeName:<#(UIColor *)#>,NSFontAttributeName : [UIFont systemFontOfSize:11.0]};
[view.tabBarItem setTitleTextAttributes:dic2 forState:UIControlStateSelected];
//UITabBarControllers 子类中
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorW,NSFontAttributeName:Font(9.0f)} forState:UIControlStateNormal];
添加元素
NSArray *viewControllers = @[view1,view2,nav3,nav4,view5,view6,nav7];
tabBarController.viewControllers = viewControllers;
//设置默认选中
tabBarController.selectedIndex = 2;
tabBarController.selectedViewController = viewControllers[2];
//类型
tabBarController.tabBar.barStyle = UIBarStyleDefault;
//颜色
tabBarController.tabBar.barTintColor = <#(UIColor *)#>;
//设置背景图片
tabBarController.tabBar.backgroundImage = <#(UIImage *)#>;
tabBarController.delegate = self;
代理方法
//点击Edit
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers {
}
//点击Done即将完成时
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
}
//点击视图控制器
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
}
//点击Done
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
}