UITabBarController/分栏控制器/标签栏控制器

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值