简介
跟UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是QQ、微信等应用.
属性
- UITabBarItem
UITabBar上面显示的每一个Tab都对应着一个ViewController,我们可以通过设置viewcontroller.tabBarItem属性来改变tabbar上对应的tab显示内容。否则系统将会根据viewController的title自动创建一个,该tabBarItem只显示文字,没有图像。当我们自己创建UITabBarItem的时候,我们可以显示的指定显示的图像和对应的文字描述。当然还可以通过setFinishedSelectedImage:withFinishedUnselectedImage:方法给选中状态和非选中状态指定不同的图片
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2];
[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]
withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
viewController2.tabBarItem = item;
- UITabBar(高度为49)
- UITabBar自己有一些方法是可以改变自身状态,但是对于UITabBarController自带的tabBar,我们不能直接去修改其状态。任何直接修改tabBar的操作将会抛出异常;
简单使用1(AppDelegate)
// 1.创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 2.设置窗口的根控制器
UITabBarController *tabBarVc = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarVc;
// 添加子控制器
UIViewController *vc = [[UIViewController alloc] init];
vc.tabBarItem.title = @"消息";
//设值显示的小红点
vc.tabBarItem.badgeValue = @"1000";
vc.tabBarItem.image = [UIImage imageNamed:@"tab_recent_nor"];
// 在iOS7之后,系统默认会把选中的按钮自动渲染.
vc.view.backgroundColor = [UIColor redColor];
[tabBarVc addChildViewController:vc];
// tabBar控制器会把自己的子控制器的view添加到自己身上
// 默认tabBar控制器会把自己的第一个子控制器的view添加的自己身上
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor greenColor];
vc1.tabBarItem.title = @"联系人";
[tabBarVc addChildViewController:vc1];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor yellowColor];
[tabBarVc addChildViewController:vc2];
// 3.显示窗口
[self.window makeKeyAndVisible];
return YES;
结合导航控制器
- 创建分栏控制器:
// 创建分栏控制器管理的子视图控制器
- (void)createViewControllers
{
OneViewController *oneVC = [[OneViewController alloc] init];
MyNavgationController *navCtrl1 = [[MyNavgationController alloc] initWithRootViewController:oneVC];
// navCtrl1.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
TwoViewController *twoVC = [[TwoViewController alloc] init];
UINavigationController *navCtrl2 = [[UINavigationController alloc] initWithRootViewController:twoVC];
ThreeViewController *threeVC = [[ThreeViewController alloc] init];
FourViewController *fourVC = [[FourViewController alloc] init];
FiveViewController *fiveVC = [[FiveViewController alloc] init];
SixViewController *sixVC = [[SixViewController alloc] init];
// 分栏控制器管理的视图控制器的tabBarController属性,自动指向分栏控制器。
// 当分栏控制器管理的视图控制器的个数超过五个时,会自动创建一个more的导航控制器,并且自动将第五个以及以后的视图控制器添加到more导航控制器中。
self.viewControllers = @[navCtrl1, navCtrl2, threeVC, fourVC, fiveVC, sixVC];
}
- 在MyNavgationController里面,重写跳转方法 :
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//1. 取出分栏
UITabBar *tabBar = self.tabBarController.tabBar;
// 将frame左移分栏的宽度
CGRect frame = tabBar.frame;
frame.origin.x -= tabBar.frame.size.width;
// 动画影藏tabBar
[UIView animateWithDuration:0.28 animations:^{
tabBar.frame = frame;
}];
[super pushViewController:viewController animated:animated];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
//1. 取出分栏
UITabBar *tabBar = self.tabBarController.tabBar;
// 将frame左移分栏的宽度
CGRect frame = tabBar.frame;
frame.origin.x += tabBar.frame.size.width;
// 动画影藏tabBar
[UIView animateWithDuration:0.28 animations:^{
tabBar.frame = frame;
}];
return [super popViewControllerAnimated:YES];
}
在自定义tabbarController
//在继承自UITabBarController
-(void)addTabBarSubContrller{
NSMutableArray *mutArray = [[NSMutableArray alloc]init];
//主页
HHHomeViewController *home = [[HHHomeViewController alloc]init];
UINavigationController *navHome = [[UINavigationController alloc]initWithRootViewController:home];
[mutArray addObject:navHome];
//团购优惠也
HHGroupViewController *group = [[HHGroupViewController alloc]init];
UINavigationController *navGroup = [[UINavigationController alloc]initWithRootViewController:group];
[mutArray addObject:navGroup];
//发现
HHFinderViewController *finder = [[HHFinderViewController alloc]init];
UINavigationController *navFinder = [[UINavigationController alloc]initWithRootViewController:finder];
[mutArray addObject:navFinder];
//我的个人中心也
HHMyMessagesViewController *myMessage = [[HHMyMessagesViewController alloc]init];
UINavigationController *navMyMessage = [[UINavigationController alloc]initWithRootViewController:myMessage];
[mutArray addObject:navMyMessage];
//添加到tabbar中
[self setViewControllers:mutArray];
}
//在子控制器中
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIImage *norimg = [UIImage imageNamed:@"main_index_home_normal"];
UIImage *selectImg = [UIImage imageNamed:@"main_index_home_pressed"];
norimg = [UIImage scaleImage:norimg toScale:0.4];
selectImg = [UIImage scaleImage:selectImg toScale:0.4];
UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:@"首页" image:norimg selectedImage:selectImg];
self.tabBarItem = item;
// item.imageInsets = UIEdgeInsetsMake(10, 10, 10, 10);
}
return self;
}