UITabBarController 能够控制一组UIViewController (或者UINavigationController)
UITabBarController 的创建和添加UIViewController步骤:
- (void)viewDidLoad {
[super viewDidLoad];
// 创建视图控制器
OneViewController *oneVC = [[OneViewController alloc] init];
TwoViewController *twoVC = [[TwoViewController alloc] init];
ThreeViewController *threeVC = [[ThreeViewController alloc] init];
FourViewController *fourVC = [[FourViewController alloc] init];
FiveViewController *fiveVC = [[FiveViewController alloc] init];
SixViewController *sixVC = [[SixViewController alloc] init];
// 创建导航控制器
UINavigationController *oneNav = [[UINavigationController alloc] initWithRootViewController:oneVC];
UINavigationController *twoNav = [[UINavigationController alloc] initWithRootViewController:twoVC];
UINavigationController *threeNav = [[UINavigationController alloc] initWithRootViewController:threeVC];
UINavigationController *fourNav = [[UINavigationController alloc] initWithRootViewController:fourVC];
UINavigationController *fiveNav = [[UINavigationController alloc] initWithRootViewController:fiveVC];
UINavigationController *sixNav = [[UINavigationController alloc] initWithRootViewController:sixVC];
// 显示按钮的标题(tabBarItem)
oneVC.tabBarItem.title = @"首页";
twoVC.tabBarItem.title = @"第二页";
threeVC.tabBarItem.title = @"第三页";
fourVC.tabBarItem.title = @"第四页";
fiveVC.tabBarItem.title = @"第五页";
sixVC.tabBarItem.title = @"第六页";
// 设置图片
// 显示图片原有颜色
// 按原始的图片进行绘制(绘制出来的是原来的颜色)
oneVC.tabBarItem.image = [[UIImage imageNamed:@"11"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
twoVC.tabBarItem.image = [UIImage imageNamed:@"24-gift"];
threeVC.tabBarItem.image = [UIImage imageNamed:@"21-skull"];
fourVC.tabBarItem.image = [UIImage imageNamed:@"61-brightness"];
fiveVC.tabBarItem.image = [UIImage imageNamed:@"99-umbrella"];
sixVC.tabBarItem.image = [UIImage imageNamed:@"64-zap"];
// 设置选中的图片
oneVC.tabBarItem.selectedImage = [UIImage imageNamed:@"88-beermug"];
twoVC.tabBarItem.selectedImage = [UIImage imageNamed:@"93-thermometer"];
threeVC.tabBarItem.selectedImage = [UIImage imageNamed:@"67-tshirt"];
fourVC.tabBarItem.selectedImage = [UIImage imageNamed:@"101-gameplan"];
fiveVC.tabBarItem.selectedImage = [UIImage imageNamed:@"108-badge"];
sixVC.tabBarItem.selectedImage = [UIImage imageNamed:@"109-chicken"];
// 显示到tabBarControl里面
// 需要添加到 viewControllers的数组中
self.viewControllers = @[oneNav, twoNav, threeNav, fourNav, fiveNav, sixNav];
// 设置bar的颜色
self.tabBar.barTintColor = [UIColor blueColor];
// 模块图片的填充色
self.tabBar.tintColor = [UIColor orangeColor];
// 设置bar背景图片
// tabBar的高度 49
self.tabBar.backgroundImage = [UIImage imageNamed:@"tabBar"];
// 设置默认选中
self.selectedIndex = 2;
// tabBarItem上的 红色提示按钮
twoVC.tabBarItem.badgeValue = @"99+";
// 设置代理
self.delegate = self;
// 释放
[oneVC release];
[twoVC release];
[threeVC release];
[fourVC release];
[fiveVC release];
[sixVC release];
[oneNav release];
[twoNav release];
[threeNav release];
[fourNav release];
[fiveNav release];
[sixNav release];
}
协议方法
#pragma mark -- 代理方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
// 可以指定哪个控制器不让点击
// 先取出 不让点击的控制器
UINavigationController *nav = self.viewControllers[3];
// 如果选中的控制器是你不想让用户点击的 那么返回NO
if (viewController == nav) {
return NO;
}else{
return YES;
}
}
// 选中页面时触发的方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// 打印选中的索引
NSLog(@"%ld",self.selectedIndex);
// 选中时 把红点去除
viewController.tabBarItem.badgeValue = nil;
}
// 控制more的代理方法
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers
{
NSLog(@"将要开始编辑more");
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
{
NSLog(@"将要结束编辑more");
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
{
NSLog(@"已经结束编辑more");
}