UITabBarController详解
UITabBarController使用详解
UITabBarController是IOS中很常用的一个viewController,例如系统的闹钟程序,ipod程序等。UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的container viewController中。
首先我们看一下它的view层级图:

一、手动创建UITabBarController
最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLaunching:方法,因为UITabBarController通常是作为整个程序的rootViewController的,我们需要在程序的window显示之前就创建好它,具体步骤如下:
1、创建一个UITabBarController对象
2、创建tabbarcontroller中每一个tab对应的要显示的对象
3、通过UITabBarController的viewController属性将要显示的所有content viewcontroller添加到UITabBarController中
4、通过设置UITabBarController对象为window.rootViewController,然后显示window
下面看一个简单的例子:
二、UITabBarItem
UITabBar上面显示的每一个Tab都对应着一个ViewController,我们可以通过设置viewcontroller.tabBarItem属性来改变tabbar上对应的tab显示内容。否则系统将会根据viewController的title自动创建一个,该tabBarItem只显示文字,没有图像。当我们自己创建UITabBarItem的时候,我们可以显示的指定显示的图像和对应的文字描述。当然还可以通过setFinishedSelectedImage:withFinishedUnselectedImage:方法给选中状态和飞选中状态指定不同的图片。下面看个自己创建UITabBarItem的小例子:
if (tabBarController && [kAPPDELEGATE.navigationController.viewControllers containsObject:tabBarController]) {
[self.navigationController popToViewController:tabBarController animated:YES];
return;
}
[kAPPDELEGATE.navigationController popToRootViewControllerAnimated:NO];
NSMutableArray *controllers=[[NSMutableArray alloc]init];
//初始化controllers
{
ViewControllerOne *one=[[ViewControllerOne alloc]init];
UITabBarItem *itemOne=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"pic_people"] selectedImage:[UIImage imageNamed:@"more_press"]];
one.tabBarItem=itemOne;
[itemOne setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateHighlighted];
[controllers addObject:one];
}
//初始化controllers
{
ViewControllerTwo *one=[[ViewControllerTwo alloc]init];
UITabBarItem *itemOne=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"pic_people"] selectedImage:[UIImage imageNamed:@"more_press"]];
one.tabBarItem=itemOne;
[itemOne setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateHighlighted];
[controllers addObject:one];
}
//初始化controllers
{
ViewControllerThree *one=[[ViewControllerThree alloc]init];
UITabBarItem *itemOne=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"pic_people"] selectedImage:[UIImage imageNamed:@"more_press"]];
one.tabBarItem=itemOne;
[itemOne setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateHighlighted];
[controllers addObject:one];
}
//初始化controllers
{
ViewControllerFour *one=[[ViewControllerFour alloc]init];
UITabBarItem *itemOne=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"pic_people"] selectedImage:[UIImage imageNamed:@"more_press"]];
one.tabBarItem=itemOne;
[itemOne setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateHighlighted];
[controllers addObject:one];
}
tabBarController=[[UITabBarController alloc]init];
tabBarController.delegate=self;
tabBarController.tabBar.clipsToBounds = TRUE;
[tabBarController setViewControllers:controllers];
//The tint color to apply to the tab bar background. 这个颜色应用于标签栏背景
[tabBarController.tabBar setBarTintColor:[UIColor greenColor]];
[kAPPDELEGATE.navigationController pushViewController:tabBarController animated:YES];
此外UITabBarItem还有一个属性badgeValue,通过设置该属性可以在其右上角显示一个小的角标,通常用于提示用户有新的消息,使用很简单,后面有例子。
三、moreNavigationController
UITabBar上最多可以显示5个Tab,当我们往UITabBarController中添加超过的viewController超过5个时候,最后一个一个就会自动变成
,按照设置的viewControlles的顺序,显示前四个viewController的tabBarItem,后面的tabBarItem将不再显示。当点击more时候将会弹出一个标准的navigationViewController,里面放有其它未显示的的viewController,并且带有一个edit按钮,通过点击该按钮可以进入类似与ipod程序中设置tabBar的编辑界面。编辑界面中默认所有的viewController都是可以编辑的,我们可以通过设置UITabBarController的customizableViewControllers属性来指定viewControllers的一个子集,即只允许一部分viewController是可以放到tabBar中显示的。但是这块儿要注意一个问题就是每当UITabBarController的viewControllers属性发生变化的时候,customizableViewControllers就会自动设置成跟viewControllers一致,即默认的所有的viewController都是可以编辑的,如果我们要始终限制只是某一部分可编辑的话,记得在每次viewControlles发生改变的时候,重新设置一次customizableViewControllers。
四、UITabBarController的Rotation
UITabBarController默认只支持竖屏,当设备方向放生变化时候,它会查询viewControllers中包含的所有ViewController,仅当所有的viewController都支持该方向时,UITabBarController才会发生旋转,否则默认的竖向。
此处需要注意当UITabBarController支持旋转,而且发生旋转的时候,只有当前显示的viewController会接收到旋转的消息。
五、UITabBar
UITabBar自己有一些方法是可以改变自身状态,但是对于UITabBarController自带的tabBar,我们不能直接去修改其状态。任何直接修改tabBar的操作将会抛出异常,下面看一个抛出异常的小例子:

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate = self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil]; self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible]; self.tabBarController.tabBar.selectedItem = nil;

上面代码的最后一行直接修改了tabBar的状态,运行程序回得到如下结果:

六、Change Selected Viewcontroller
改变UITabBarController中当前显示的viewController,可以通过一下两种方法:
1、selectedIndex属性
通过该属性可以获得当前选中的viewController,设置该属性,可以显示viewControllers中对应的index的viewController。如果当前选中的是MoreViewController的话,该属性获取出来的值是NSNotFound,而且通过该属性也不能设置选中MoreViewController。设置index超出viewControllers的范围,将会被忽略。
2、selectedViewController属性
通过该属性可以获取到当前显示的viewController,通过设置该属性可以设置当前选中的viewController,同时更新selectedIndex。可以通过给该属性赋值
tabBarController.moreNavigationController可以选中moreViewController。
3、viewControllers属性
设置viewControllers属性也会影响当前选中的viewController,设置该属性时UITabBarController首先会清空所有旧的viewController,然后部署新的viewController,接着尝试重新选中上一次显示的viewController,如果该viewController已经不存在的话,会接着尝试选中index和selectedIndex相同的viewController,如果该index无效的话,则默认选中第一个viewController。
七、UITabBarControllerDelegate
通过代理我们可以监测UITabBarController的当前选中viewController的变化,以及moreViewController中对编辑所有viewController的编辑。通过实现下面方法:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
该方法用于控制TabBarItem能不能选中,返回NO,将禁止用户点击某一个TabBarItem被选中。但是程序内部还是可以通过直接setSelectedIndex选中该TabBarItem。
下面这三个方法主要用于监测对moreViewController中对view controller的edit操作
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers; - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed; - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
完整实例:
-(void)initTabViewController{
if (tabBarController && [kAPPDELEGATE.navigationController.viewControllers containsObject:tabBarController]) {
[self.navigationController popToViewController:tabBarController animated:YES];
return;
}
[kAPPDELEGATE.navigationController popToRootViewControllerAnimated:NO];
NSMutableArray *controllers=[[NSMutableArray alloc]init];
//初始化controllers
{
ViewControllerOne *one=[[ViewControllerOne alloc]init];
UITabBarItem *itemOne=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"pic_people"] selectedImage:[UIImage imageNamed:@"more_press"]];
one.tabBarItem=itemOne;
[itemOne setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateHighlighted];
[controllers addObject:one];
}
//初始化controllers
{
ViewControllerTwo *one=[[ViewControllerTwo alloc]init];
UITabBarItem *itemOne=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"pic_people"] selectedImage:[UIImage imageNamed:@"more_press"]];
one.tabBarItem=itemOne;
[itemOne setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateHighlighted];
[controllers addObject:one];
}
//初始化controllers
{
ViewControllerThree *one=[[ViewControllerThree alloc]init];
UITabBarItem *itemOne=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"pic_people"] selectedImage:[UIImage imageNamed:@"more_press"]];
one.tabBarItem=itemOne;
[itemOne setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateHighlighted];
[controllers addObject:one];
}
//初始化controllers
{
ViewControllerFour *one=[[ViewControllerFour alloc]init];
UITabBarItem *itemOne=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"pic_people"] selectedImage:[UIImage imageNamed:@"more_press"]];
one.tabBarItem=itemOne;
[itemOne setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateHighlighted];
[controllers addObject:one];
}
tabBarController=[[UITabBarController alloc]init];
tabBarController.delegate=self;
tabBarController.tabBar.clipsToBounds = TRUE;
[tabBarController setViewControllers:controllers];
//The tint color to apply to the tab bar background. 这个颜色应用于标签栏背景
[tabBarController.tabBar setBarTintColor:[UIColor greenColor]];
[kAPPDELEGATE.navigationController pushViewController:tabBarController animated:YES];
}
UITabBarController详解的更多相关文章
- iOS开发——控制器OC篇&UINavigationController&UITabBarController详解
UINavigationController&UITabBarController详解 一:UINavigationController 控制器的属性: UINavigationControl ...
- UI第十六节——UITabBarController详解
一.UITabBarController主要用来管理你提供的content view controllers,而每一个 content view controller则负责管理自己的view层级关系, ...
- UITabBarController 详解之 hidesBottomBarWhenPushed的正确用法
今天说的是在TabBar嵌套Nav时,进行Push的时候隐藏TabBar的问题. 之前项目也需要这么做,那时候iOS7还没出,也是各种搜罗,后来的解决方法是当push操作的时候自己隐藏Tabbar,p ...
- iOS开发-UITabBarController详解
我们在开发中经常会使用到UITabBarController来布局App应用,使用UITabBarController可以使应用看起来更加的清晰,iOS系统的闹钟程序,ipod程序都是非常好的说明和A ...
- UITabBarController 详解
// UITabBarController 标签视图控制 // 主要管理没有层级关系的视图控制器 // 1. ViewControllers 所有被管理的视图控制器, 都在这个数组中 // 2. se ...
- iOS 视图控制器转场详解
iOS 视图控制器转场详解 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标有了大幅度的增长,多谢唐巧前辈的推荐.有些人问我相关的问题 ...
- iOS应用开发详解
<iOS应用开发详解> 基本信息 作者: 郭宏志 出版社:电子工业出版社 ISBN:9787121207075 上架时间:2013-6-28 出版日期:2013 年7月 开本:16开 ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
随机推荐
- Use powerful plugins in your vim.
# setup by root wget http://prdownloads.sourceforge.net/ctags/ctags-5.8.tar.gz tar -xzvf ctags-5.8.t ...
- CodeWars可以学习的
http://www.codewars.com/kata/54ff3102c1bad923760001f3/solutions/csharp 判断给定的字符串有多少个a e i o u using S ...
- How does it work in C#? - Part 3 (C# LINQ in detail)
http://www.codeproject.com/Articles/383749/How-does-it-work-in-Csharp-Part-Csharp-LINQ-in-d
- Android开发之内容观察者
内容观察者: 当关注应用的数据库数据改变时,内容提供者会发出通知,在内容提供者的uri上注册一个内容观察者,就可以收到数据改变的通知 实现步骤: 1.假如是自定义的ContentProvider,需要 ...
- 几种java通信(rmi,http,hessian,webservice)协议性能比较
一.综述 本文比较了RMI,Hessian,Burlap,Httpinvoker,web service等5种通讯协议的在不同的数据结构和不同数据量时的传输性能.RMI是java语言本身提供的通讯协议 ...
- 函数buf_LRU_block_remove_hashed_page
/******************************************************************//** Takes a block out of the LRU ...
- POJ 3648 Wedding (2-SAT,经典)
题意:新郎和新娘结婚,来了n-1对夫妻,这些夫妻包括新郎之间有通奸关系(包括男女,男男,女女),我们的目地是为了满足新娘,新娘对面不能坐着一对夫妻,也不能坐着有任何通奸关系的人,另外新郎一定要坐新娘对 ...
- CI的知识点
1. 超级对象中$this->uri 获取pathinfo中的值,$this->uri->segment(n)中的n表示第几个参数 如:/welcome/index/1 使用$th ...
- Spring depends-on介绍
<!-- redis配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.Jedis ...
- Java [leetcode 33]Search in Rotated Sorted Array
题目描述: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...