iOS——TabbarViewConttoller

UITabBarController 继承自UIViewController ,一出来就有一个视图,这个视图上又两个控件, contentView和TabBar ,是用来管理多个单视图控制器的多视图控制器,它管理的多个单视图控制器之间是并列的关系,(同时存在,但是相互之间没有太大的关联)
UITabBarController 管理的视图控制器对象自带的 view 只有当它第一次出现时会创建,以后就不再创建了,所以标签视图控制器管理的视图控制器之间是并列存在的

由于每个标签栏分别控制一个单视图控制器,因此需要分别对每个单视图控制器进行配置属性

例如: 我创建了四个单视图控制器 firstVC secondVC thirdVC forthVC
这里写图片描述
这里写图片描述
//firstVC
FirstViewController *firstVC = [[FirstViewController alloc]init];
// 配置标签栏的标题
firstVC.tabBarItem.title = @”firstVC”;
// 配置标签栏中的图片
firstVC.tabBarItem.image = [UIImage imageNamed:@”01-refresh”];
// 配置右上角的角标(信息数量)
firstVC.tabBarItem.badgeValue = @”99+”;

//secondVC
SecondViewController *secondVC = [[SecondViewController alloc]init];
secondVC.tabBarItem.title = @”secondVC”;
secondVC.tabBarItem.image = [UIImage imageNamed:@”02-redo”];
secondVC.tabBarItem.badgeValue = @”点我”;
// 配置标签栏的唯一标识
secondVC.tabBarItem.tag = 101;

//thirdVC
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
thirdVC.tabBarItem.title = @”thirdVC”;
thirdVC.tabBarItem.image = [UIImage imageNamed:@”03-loopback”];

//forthVC
ForthViewController *forthVC = [[ForthViewController alloc]init];
forthVC.tabBarItem.title = @”forthVC”;
forthVC.tabBarItem.image = [UIImage imageNamed:@”04-squiggle”];

UITabBarController *tbController = [[UITabBarController alloc]init];

// 准备一个数组,存放视图控制器
NSArray *controllers = @[firstVC,secondVC,thirdVC,forthVC];

// 1. 配置所管理的多个单视图控制器
tbController.viewController = controllers;
// 2. 配置标签栏的颜色
tbController.tabBar.barTintColor = [UIColor cyanColor];
// 3. 配置内容的渲染颜色(选中后的标签颜色)
tbController.tabBar.tintColor = [UIColor magentaColor];
// 4. 配置标签栏的图片
// 标签栏的高度是49
tbController.tabBar.backgroudImage = [UIImage imageNamed:@“1”];
// 5. 配置默认选中的标签(有两种方法)
// 根据下标选择
tbController.selectedIndex = 0;
// 根据视图控制器来选择
tbController.selectedViewController = firstVC;
// 6. 配置代理属性
tbController.delegate = self;

还有两个代理方法:

// 当切换标签的时候触发,询问当前的标签是否可以选中
- (BOOL)tabBarController:(UITabBarController )tabBarController shouldSelectViewController:(UIViewController )viewController NS_AVAILABLE_IOS(3_0){
// viewController 将要选中的视图
// tabBarController 指的是当前的代理委托者
// 使第二个视图不可被选中
// if(101 == viewController.tabBarItem.tag){
// return NO;
// }

return YES;

}

// 触发时机:当标签选中的时候触发
- (void)tabBarController:(UITabBarController )tabBarController didSelectViewController:(UIViewController )viewController{
// 此时,当前选中的标签就是用 viewController 代表

viewController.tabBarItem.badgeValue = nil;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值