理解:和UINavigationController一样,UITabBarController也可以控制多个页面导航。用户可以在多个视图控制器之间移动,并可以定制屏幕底部的选项卡栏。
UITabBarController是组建一系列的控制器,可以是UIViewController,UITableViewController,UINavigationController等,并将它们添加到选项卡栏,使每一个选项卡对应一个控制器。UINavigationController是以栈的形式将视图推入推出。UITabbarController一般作为appDelegate的根rootController使用。
分析图:
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) TabBarViewController * tabbarController;
.m中,AViewController *aController = [[AViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:aController];
//aController.title = @"aa";
BViewController *bController = [[BViewController alloc] init];
// bController.title = @"bb";
CViewController *cController = [[CViewController alloc] init];
// cController.title = @"cc";
DViewController *dController = [[DViewController alloc] init];
// dController.title = @"dd";
_tabbarController = [[TabBarViewController alloc] initWithNibName:@"TabBarViewController" bundle:nil];
_tabbarController.viewControllers = [NSArray arrayWithObjects:nav,bController,cController,dController, nil];
//设置tabbar
//适合ios5以下
UIImageView *tabbarGroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainbar_bg.png"]];
[tabbarGroundImage setFrame:CGRectMake(0,0, self.tabBar.frame.size.width, self.tabBar.frame.size.height)];
//ios5以下使用
[self.tabBar insertSubview:tabbarGroundImage atIndex:0];
[tabbarGroundImage release];
//适和ios5以上
[_tabbarController.tabBar setBackgroundImage:[UIImage imageNamed:@"mainbar_bg.png"]];
UITabBarItem *aTabbar = [[UITabBarItem alloc] initWithTitle:nil image:[UIImage imageNamed:@"mainbar_icon1_n.png"] tag:0];
[aTabbar setFinishedSelectedImage:[UIImage imageNamed:@"mainbar_icon1_d.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"mainbar_icon1_h.png"]];
aController.tabBarItem = aTabbar;
[aTabbar release];
[aController release];
[nav release];
[bController release];
[cController release];
[dController release];
self.window.rootViewController = _tabbarController;
注:用TabbarController上的tabbar,backGroundImage和tabbarItem上的image都是不能改变大小的,所以对切图要求很高。
EViewController *eController = [[EViewController alloc] initWithNibName:@"EViewController" bundle:nil];
//在push之前加上这句话,放可管用。
eController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:eController animated:YES];
[eController release];