UITabBarController/分栏控制器/标签栏控制器
【注】标签栏控制器,是UIKit框架提供的一个容器视图控制器,用于切换相同级别的兄弟视图控制器。iOS允许将导航控制器添加到标签栏控制器中进行管理(事实上这种结构非常常见),但原则上,不推荐将分栏控制器添加到导航控制器中进行管理。
一.基本的切换方式
【注】标签栏的高度 49
vc.hidesBottomBarWhenPushed = YES;
我们的标签栏最多只能放5个Controller
二.定制
【注】(1)TabBarViewController本身只有一个,无需定制
(2)TabBar属于TabBarViewController,用于显示每个标签,可以定制
(3)每个视图控制器拥有属于自己的TabItem(标签),需要定制
1、创建标签栏控制器
UITabBarController *tbc=[[UITabBarController alloc] init];
2、根视图,就是个标签栏控制器
UITabBarController *tbc=(id)self.window.rootViewController;
3、设置tabbar的颜色
tbc.tabBar.tintColor=[UIColor orangeColor];
4、设置tabbar背景颜色
(1)x-code7版本前的方法
[tbc.tabBar setBackgroundColor:[UIColor greenColor]];
(2)x-code7版本后的方法
if (7<=[[UIDevice currentDevice].systemVersion floatValue]) {
tbc.tabBar.barTintColor=[UIColor greenColor];
}
5、设置透明
tbc.tabBar.translucent=YES;
6、设置tabbar的风格(透明为NO,并注释掉tabber背景颜色)
tbc.tabBar.barStyle=UIBarStyleBlack;
UIBarStyleDefault = 0,
UIBarStyleBlack = 1,
7、添加背景图片
(1)x-code7版本前的方法
[tbc.tabBar setBackgroundImage:[UIImage imageNamed:@"header_bg.png"]];
(2)x-code7版本后的方法
UIImage *image=[UIImage imageNamed:@"header_bg.png"];
if (7<=[[UIDevice currentDevice].systemVersion floatValue]) {
image=[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImageRenderingModeAutomatic, // Use the default rendering mode for the context where the image is used
UIImageRenderingModeAlwaysOriginal, // Always draw the original image, without treating it as a template 根据图片绘制原始状态
UIImageRenderingModeAlwaysTemplate, // Always draw the image as a template image, ignoring its color information 根据tintColor来绘制
}
[tbc.tabBar setBackgroundImage:image];
8、定制tabBar的样式
(1)设置系统自带样式
v1.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
(2)根据图片设置样式
v2.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"第2页" image:[UIImage imageNamed:@"tab_1"] tag:1];
(3)根据图片设置样式(点击之前显示的第二个参数的图片,点击之后显示第三个参数的图片)
v3.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"第3页" image:[UIImage imageNamed:@"tab_3"] selectedImage:[UIImage imageNamed:@"tab_c0"]];
9、设置偏移量(水平,垂直),正数为下为右,负数为上为左
nc.tabBarItem.titlePositionAdjustment=UIOffsetMake(5, -10);
10、设置右上角的数字(小红点)
v2.tabBarItem.badgeValue=[NSString stringWithFormat:@"2"];
三.协议方法
UITabBarControllerDelegate
四.自制TabBar