UITabBarController也是和UINaVigationController一样是多视图控制器 ,但是他和导航视图控制器不太一样,在对视图控制器管理上 ,导航视图控制器通过push pop进行栈的管理 ,界面之间是有层级关系,
标签视图控制器 管理的视图控制器是并列存在的关系 我们通过点击不同的标签 进行不同界面的切换
在根视图控制器
//配置ViewControllers
-(void)configViewControllers{
//第一个
First *firstVC = [[[First alloc]init]autorelease];
//导航视图控制器
UINavigationController *firstNaVC = [[[UINavigationController alloc]initWithRootViewController:firstVC]autorelease];
// //设置导航栏title
// firstVC.navigationItem.title = @"主页";
// //设置tabBar的title
// firstNaVC.tabBarItem.title = @"first";
// 设置导航栏title和下面的导航title 等同于上面两步
firstVC.title = @"主页";
//设置tabBarItem图片
firstNaVC.tabBarItem.image = [UIImage imageNamed:@"53-house.png"];
//第二个
Second *SecondVC = [[[Second alloc]init]autorelease];
//导航视图控制器
UINavigationController *SecondNaVC = [[[UINavigationController alloc]initWithRootViewController:SecondVC]autorelease];
//设置导航栏title和下面的导航title
SecondVC.title = @"海贼";
SecondNaVC.tabBarItem.image = [UIImage imageNamed:@"22-skull-n-crossbones.png"];
//第三个
Third *ThirdVC = [[[Third alloc]init]autorelease];
//导航视图控制器
UINavigationController *ThirdNaVC = [[[UINavigationController alloc]initWithRootViewController:ThirdVC]autorelease];
ThirdVC.title = @"电话";
ThirdNaVC.tabBarItem.image = [UIImage imageNamed:@"75-phone.png"];
//增加红点提示
ThirdNaVC.tabBarItem.badgeValue = @"99+";
Fourth *forthVC = [[[Fourth alloc]init]autorelease];
//导航视图控制器
UINavigationController *forthNaVC = [[[UINavigationController alloc]initWithRootViewController:forthVC]autorelease];
forthVC.title = @"百度";
forthNaVC.tabBarItem.image = [UIImage imageNamed:@"82-dogpaw.png"];
//第五个
Fiveth *fiveVC = [[[Fiveth alloc]init]autorelease];
//导航视图控制器
UINavigationController *fiveNaVC = [[[UINavigationController alloc]initWithRootViewController:fiveVC]autorelease];
fiveVC.title = @"爱";
fiveNaVC.tabBarItem.image = [UIImage imageNamed:@"29-heart.png"];
//设置 tabBarController 控制的视图控制器们
self.viewControllers = @[firstNaVC,SecondNaVC,ThirdNaVC,forthNaVC,fiveNaVC];
}
点击取消红点提示 (AppDelegate.m文件)
1:在 AppDelegate.m文件遵守 UITabBarControllerDelegate协议
2 : 设置代理
tabRootVC.delegate = self;
3:实现两个方法
//设置Controller对应的标签 是否可以点击 是否可以调换位置
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
return YES;
}
设置TabBar
//tintcolor tabBar选中颜色
tabRootVC.tabBar.tintColor = [UIColor cyanColor];
//barTintColor tabBar背景
tabRootVC.tabBar.barTintColor = [UIColor blackColor];
//设置图片 必须给49高度
[tabRootVC.tabBar setBackgroundImage:[UIImage imageNamed:@"320x49.png"]];