标签视图控制器 是一种多视图控制器 ,管理具有并列关系的视图控制器,同时存在。一旦视图视图控制器的界面超过五个时,自动将最后一个标签换成more,剩余的标签全部在more中显示
标签视图控制器属性配置
这里为了方便下面使用,我们首先定义几个视图控制器
FirstController *first = [[FirstController alloc] init];
//设置标签栏的title
first.tabBarItem.title = @"朋友圈";
//设置标签栏的图片
first.tabBarItem.image = [UIImage imageNamed:@"tabbar_me@2x"];
//设置标签选中时的颜色
first.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_mainframe@2x"];
//设置标签右上角角标
first.tabBarItem.badgeValue = @"99+";
//设置标签的唯一标示
first.tabBarItem.tag = 111;
SecondViewController *second = [[SecondViewController alloc] init];
second.tabBarItem.title = @"发现";
second.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"];
second.tabBarItem.tag = 222;
ThirdViewController *third = [[ThirdViewController alloc] init];
third.tabBarItem.title = @"联系人";
third.tabBarItem.image = [UIImage imageNamed:@"tabbar_contacts"];
ForthViewController *forth = [[ForthViewController alloc] init];
forth.tabBarItem.title = @"消息";
forth.tabBarItem.image = [UIImage imageNamed:@"tabbar_contacts"];
FifthViewController *fifth = [[FifthViewController alloc] init];
fifth.tabBarItem.title = @"收藏";
fifth.tabBarItem.image = [UIImage imageNamed:@"tabbar_badge@2x"];
SixthViewController *sixth = [[SixthViewController alloc] init];
sixth.tabBarItem.title = @"美食";
下面我们配置属性
// UITabBarController 标签视图控制器
//多视图控制器 管理具有并列关系的视图控制器,同时存在 一旦视图控制器的界面超过五个时,自动将最后一个标签换成more,剩余的标签全部在more中显示
UITabBarController *tabBarVC =[[UITabBarController alloc] init];
//1.配置管理的视图控制器
tabBarVC.viewControllers = @[first ,second, third, forth, fifth, sixth];
//2.标签栏的颜色
tabBarVC.tabBar.barTintColor = [UIColor greenColor];
//3.标签栏的渲染颜色
tabBarVC.tabBar.tintColor = [UIColor blueColor];
//4.设置标签栏的背景图片
tabBarVC.tabBar.backgroundImage = [UIImage imageNamed:@"background"];
//5.设置默认的选中标签
tabBarVC.selectedIndex = 1;//默认为0
tabBarVC.selectedViewController = third;
//6.设置delegate
tabBarVC.delegate = self;
//指定为window的根视图控制器
self.window.rootViewController = tabBarVC;
[tabBarVC release];
代理的实现
#pragma mark ---UITabBarControllerDelegate---
//选中某一个标签时触发,设置是否可以被选中
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (viewController.tabBarItem.tag == 222) {
return NO;
}
return YES;
}
//已经选中标签时触发
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
//选中哪个标签,将对应角标去掉
viewController.tabBarItem.badgeValue = nil;
}
代码优化
这里我们看到在定义视图控制器时代码比较繁琐,下面我们进行代码的优化(这里我们以微信的登入界面为例)
主要使用到 NSClassFromString(classArr[i] 将字符串转化为同名的类这个方法
NSMutableArray *allVC = [NSMutableArray array];//存储所管理的视图控制器
for (int i = 0; i < 4; i ++) {
//数据源
NSArray *classArr = @[@"ChatViewController",
@"ContactsViewController",
@"DiscoverViewController",
@"MeViewController"];
//存储title
NSArray *title = @[@"微信",@"通讯录",@"发现",@"我"];
//存储图片名
NSArray *image = @[@"tabbar_mainframe",@"tabbar_contacts",@"tabbar_discover",@"tabbar_me"];
//存储选中后的图片名
NSArray *selectImage = @[@"tabbar_mainframeHL",@"tabbar_contactsHL",@"tabbar_discoverHL",@"tabbar_meHL"];
/**
NSClassFromString(classArr[i] 将字符串转化为同名的类
*/
UITableViewController *VC = [[NSClassFromString(classArr[i]) alloc]init ];
VC.tabBarItem.title =title[i];
VC.tabBarItem.image =[UIImage imageNamed:image[i]];
VC.tabBarItem.selectedImage =[UIImage imageNamed: selectImage[i]] ;
MainNavigationController * mainVC = [[MainNavigationController alloc] initWithRootViewController:VC];
[allVC addObject:mainVC];
[VC release];
[mainVC release];
}
//设置标签栏渲染颜色
self.tabBar.tintColor = [UIColor greenColor];
//标签栏的颜色
self.tabBar.barTintColor = [UIColor yellowColor];
//设置所管理的视图控制器
self.viewControllers = allVC;
另外假如我们想点击时渲染图片的原来颜色,我们可以使用image的一个方法imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
meVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_meHL"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
如果使用为MRC的环境,一定要记得释放所有权