UITabBarController是IOS中很常用的一个viewController,例如系统的闹钟程序,ipod程序等。UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的container viewController中。(试验结果是UITabBarController可以嵌入到UINavigationController中,并且UITabBarController 中还可以继续嵌入UINavigationController作为其TabItem)
首先我们看一下它的view层级图:
一、手动创建UITabBarController
最常见的创建UITabBarController的地方就是在application delegate中的
1、创建一个UITabBarController对象
2、创建tabbarcontroller中每一个tab对应的要显示的对象
3、通过UITabBarController的viewController属性将要显示的所有content viewcontroller添加到UITabBarController中
4、通过设置UITabBarController对象为window.rootViewController,然后显示window
下面看一个简单的例子:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch.
SvTabBarFirstViewController *viewController1, *viewController2;
viewController1 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil]; viewController1.title = @"First"; viewController2 = [[SvTabBarFirstViewControl ler alloc] initWithNibName:nil bundle:nil]; viewController2.title = @"Second"; self.tabBarController = [[[UITabBarController alloc] init] autorelease]; self.tabBarController.delegate = self; self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
[viewController1 release]; [viewController2 release]; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; return YES; }
二、UITabBarItem
UITabBar上面显示的每一个Tab都对应着一个ViewController,我们可以通过设置viewcontroller.tabBarItem属性来改变tabbar上对应的tab显示内容。否则系统将会根据viewController的title自动创建一个,该tabBarItem只显示文字,没有图像。当我们自己创建UITabBarItem的时候,我们可以显示的指定显示的图像和对应的文字描述。当然还可以通过setFinishedSelectedImage
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2]; [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"] withFinishedUnselectedIm age:[UIImage imageNamed:@"first.png"]]; viewController2.tabBarItem = item; [item release];
此外UITabBarItem还有一个属性badgeValue,通过设置该属性可以在其右上角显示一个小的角标,通常用于提示用户有新的消息,使用很简单,后面有例子。
三、moreNavigationController
UITabBar上最多可以显示5个Tab,当我们往UITabBarController中添加超过的viewController超过5个时候,最后一个一个就会自动变成,按照设置的viewControlles的顺序,显示前四个viewController的tabBarItem,后面的tabBarItem将不再显示。当点击more时候将会弹出一个标准的navigationViewController
四、UITabBarController的Rotation
UITabBarController默认只支持竖屏,当设备方向放生变化时候,它会查询viewControllers中包含的所有ViewController,仅当所有的viewController都支持该方向时,UITabBarController才会发生旋转,否则默认的竖向。
此处需要注意当UITabBarController支持旋转,而且发生旋转的时候,只有当前显示的viewController会接收到旋转的消息。
五、UITabBar
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
3、viewControllers属性
设置viewControllers属性也会影响当前选中的viewController,设置该属性时UITabBarController首先会清空所有旧的viewController,然后部署新的viewController,接着尝试重新选中上一次显示的viewController,如果该viewController已经不存在的话,会接着尝试选中index和selectedIndex相同的viewController,如果该index无效的话,则默认选中第一个viewController。
七、UITabBarControllerDelega
通过代理我们可以监测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 willEndCustomizingViewCo ntrollers:(NSArray *)viewControllers changed:(BOOL)changed; - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewCon trollers:(NSArray *)viewControllers changed:(BOOL)changed;
七、附件UITabBarController测试程序源码
SvTabBarAppDelegate.h这个头文件,折叠后加进去,总是无法展开,望大家见谅!
// // SvTabBarAppDelegate.h // SvTabBarControllerSample// // Created by maple on 5/19/12. // Copyright (c) 2012 smileEvday. All rights reserved. // #import @interface SvTabBarAppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UITabBarController *tabBarController; @end





// // SvTabBarSecondViewController.m // SvTabBarControllerSample // // Created by maple on 5/19/12. // Copyright (c) 2012 smileEvday. All rights reserved. // #import "SvTabBarSecondViewContro ller.h" @interface SvTabBarSecondViewContro ller () @end @implementation SvTabBarSecondViewContro ller - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.tabBarItem.image = [UIImage imageNamed:@"second"]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor greenColor]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterf aceOrientation:(UIInterfaceOrientation)interfaceOrientation { //return (interfaceOrientation == UIInterfaceOrientationPo rtrait); return YES; } @end