UITabBarController是一个容器控制器,继承于UIViewController,一般情况下作为window的根视图控制器
UITabBarController自带一个UITabBar
首先创建一个UItabBarController
UITabBarController* barTC =[[UITabBarControlleralloc]init];
作为window的根视图
self.window.rootViewController= barTC;
设置标题为首页
barTC.navigationItem.title= @"首页";
创建5个视图控制器
FirstViewController * firstVC =[[FirstViewControlleralloc]init];
SecondViewController * secondVC =[[SecondViewControlleralloc]init];
ThirdViewController * thirdVC =[[ThirdViewControlleralloc]init];
FourViewController * fourVC =[[FourViewControlleralloc]init];
FiveViewController* fiveVC =[[FiveViewControlleralloc]init];
再给5个视图添加导航控制器
UINavigationController* navigationVC1 =[[UINavigationControlleralloc]initWithRootViewController:firstVC];
UINavigationController * navigationVC2 =[[UINavigationControlleralloc]initWithRootViewController:secondVC];
UINavigationController* navigationVC3 =[[UINavigationControlleralloc]initWithRootViewController:thirdVC];
UINavigationController* navigationVC4 =[[UINavigationControlleralloc]initWithRootViewController:fourVC];
UINavigationController* navigationVC5 =[[UINavigationControlleralloc]initWithRootViewController:fiveVC];
//UITabBarController的子视图控制器
barTC.viewControllers=@[navigationVC1,navigationVC2,navigationVC3,navigationVC4,navigationVC5];
firstVC.view.backgroundColor= [UIColorredColor];
firstVC.tabBarItem.title= @"首页";
//设置角标
firstVC.tabBarItem.badgeValue= @"33";
//UItabBar会对图片进行渲染,如果不想渲染的话就需要使用(UIImageRenderingModeAlwaysOriginal)。
firstVC.tabBarItem.image= [[UIImageimageNamed:@"home.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
secondVC.view.backgroundColor= [UIColorblackColor];
secondVC.tabBarItem.title= @"发现";
secondVC.tabBarItem.image= [[UIImageimageNamed:@"find.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
thirdVC.view.backgroundColor= [UIColorwhiteColor];
thirdVC.tabBarItem.title= @"设置";
thirdVC.tabBarItem.image= [[UIImageimageNamed:@"setting.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
fourVC.view.backgroundColor= [UIColorgrayColor];
fourVC.tabBarItem.title= @"购物";
fourVC.tabBarItem.image= [[UIImageimageNamed:@"shopping.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
fiveVC.view.backgroundColor= [UIColorgreenColor];
fiveVC.tabBarItem.title= @"联系人";
fiveVC.tabBarItem.image= [UIImageimageNamed:@"contact.png"];
//
// firstVC.tabBarItem = [[[UITabBarItemalloc]initWithTabBarSystemItem:(UITabBarSystemItemBookmarks)tag:1]autorelease];
// firstVC.tabBarItem.badgeValue =@"23";
// secondVC.tabBarItem =[[[UITabBarItemalloc]initWithTabBarSystemItem:(UITabBarSystemItemContacts)tag:2]autorelease];
// thirdVC.tabBarItem = [[[UITabBarItemalloc]initWithTabBarSystemItem:(UITabBarSystemItemDownloads)tag:3]autorelease] ;
// fourVC.tabBarItem = [[[UITabBarItemalloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"find.png"]selectedImage:[UIImageimageNamed:@"map.png"]]autorelease];
// fiveVC.tabBarItem = [[[UITabBarItemalloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"home.png"]selectedImage:[UIImage imageNamed:nil]]autorelease];
// SixViewController * sixVC = [[SixViewControlleralloc]init];
// sixVC.tabBarItem = [[[UITabBarItemalloc]initWithTitle:@"猴子" image:[UIImage imageNamed:@"monkey.png"]selectedImage:nil]autorelease];
效果图如下:
三大视图控制器组成的框架结构--UItabBarController、UINavigationController、UITableViewController
第一种--UINavigationController管理UITabBarController,这种结构UINavigationBar上面的内容是不变的。
第二种结构---UItabBarController管理UINavigationController,这种结构每一个具体的视图控制器可以设置自己在navigationBar上面的内容
主要事项:1.如果要给tabBarItem,应该给对应的navigationcontroller设置
2.如果设置navigationBarItem,应该给具体的视图控制器设置。一般情况下会在视图控制器的内部方法里面设置
UITabBarControllerDelegate协议里面有如下方法,要定义代理对象为自身 barTC.delegate = self;
- (void)tabBarController:(UITabBarController *)tabBarControllerdidSelectViewController:(UIViewController *)viewController