UITabBarController

⼀、标签视图控制器 — UITabBarController
 
UITabBarController,它可以帮我们添加、管理许多的标签项, 使我们的 程序包含不同的操作模式,由于管理UITabBar可能会有些⿇烦,所以系 统也帮我们对其进⾏了封装,产⽣了简单好⽤的UITabBarController—标 签视图控制器。
 
  • 视图—>图层—>⼦视图
  • 视图控制器—>管理视图
  • 导航视图控制器—>管理有层次关系的视图控制器
  • 标签视图控制器—>管理没有层次关系的视图控制器
 
UITabBarController和UINavigationController⼀样都是继承于UIViewController
 
[firstVC.button addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];
   
    firstVC.tabBarItem.title = @"巴达";//设置显示文本
    firstVC.tabBarItem.badgeValue = @"富富";//设置显示角标
//    UIImage *image = [[UIImage imageNamed:@"iconfont-menfengguoda"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *image = [[UIImage imageNamed:@"iconfont-menfengguoda"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
   
    firstVC.tabBarItem.image = image;//设置显示图片
   
    SecondViewController *secondVC = [[SecondViewController alloc] init];
   
    //第一个参数 为标签显示的文本
    //第二个参数 正常状态下显示的图片
    //第三个参数 选中状态下显示的图片
    //注意,图片想要正常显示,必须是png类型
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"秋香" image:[UIImage imageNamed:@"iconfont-buya"] selectedImage:[UIImage imageNamed:@"iconfont-3mquanciyaicon"]];
   
    //创建好标签之后指定为某一个视图控制器的标签
    secondVC.tabBarItem = item;
    [item release];
   
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
   
    //创建一个系统样式标签
      // 参数1:系统的TabBar样式 是⼀个枚举
// 参数2:tabBarItem的tag值
    UITabBarItem *systemItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:101];
    thirdVC.tabBarItem = systemItem;
    [systemItem release];
   
    FourthViewController *fourthVC = [[FourthViewController alloc] init];
   
    //第一个参数 显示的文本
    //第二个参数 正常状态下显示的图片
    //第三个参数 标记值
    UITabBarItem *commomItem = [[UITabBarItem alloc] initWithTitle:@"洪荒" image:[UIImage imageNamed:@"iconfont-chaobotiemianicon"] tag:102];
    commomItem.badgeValue = @"1";
    fourthVC.tabBarItem = commomItem;
    [commomItem release];
   
    //创建标签视图控制器
    UITabBarController *tabbarController = [[UITabBarController alloc] init];
   
    //一键换肤,会员专用
    [[UITabBar appearance] setTintColor:[UIColor redColor]];
   
//    tabbarController.tabBar.barStyle =
   
    //设置标签视图控制器需要管理的子视图控制器
    tabbarController.viewControllers = @[firstVC, secondVC, thirdVC, fourthVC];
   
    //设定标签栏选中的标签下标
    tabbarController.selectedIndex = 1;
   
    //指定window的根视图控制器为标签视图控制器
    self.window.rootViewController = tabbarController;
   
    //释放
    [firstVC release];
    [secondVC release];
    [thirdVC release];
    [fourthVC release];
   
    [tabbarController release];
 
程序的添加过程: UIWindow—>UITabBarcontroller—>UINavigationController— >UIViewController
 
tabBar 标签栏
 
selectedIndex 选中的某个tabBarItem
 
  • 我们可以⽤协议来监听当前点击了哪个标签
 
代码:
 
// 点击某个标签时(tabBarItem)时触发该⽅法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:
(UIViewController *)viewController
{
    NSLog(@"%ld", tabBarController.selectedIndex);
    NSLog(@"viewController = %@", viewController);
}
 
⼆、UITabBar
 
  • UITabBar包含多个UITabBarItem,每⼀个UITabBarItem对 应⼀个UIViewController。UITabBar的⾼度是49。
  • 系统最多只显⽰5个UITabBarItem,当UITabBarItem超过5 个时系统会⾃动增加⼀个更多按钮,点击更多按钮没有在 底部出现的按钮会以列表的形式显⽰出来
  • UITabBar的属性:tintColor、barTintColor、图像设置 等。
 
代码:
    // 设置tabBar选中的颜⾊
    [mainTabBarVC.tabBar setTintColor:[UIColor redColor]];
    // 设置tabBar的背景颜⾊
    [mainTabBarVC.tabBar setBarTintColor:[UIColor lightGrayColor]];
    // 是否半透明 默认是YES NO为不显⽰半透明状态    mainTabBarVC.tabBar.translucent = NO;
 
 
三、⾃定义tabBar外观
 
通过UIAppearance协议来进⾏ 操作,通过它可以对⼀些控件进⾏⾃定义颜⾊等。
代码:
// 设置全局外观
// 通过[UITabBar appearance]得到当前应⽤的UITabBar对象来设置tabBar的
外观
// 注意:设置全局外观最好在appDelegate⾥,否则会⽆效
[[UITabBar appearance] setBarTintColor:[UIColor cyanColor]];
[[UITabBar appearance] setTintColor:[UIColor brownColor]];
// 改变导航栏外观颜⾊
[[UINavigationBar appearance] setBarTintColor:[UIColor lightGrayColor]];
// 改变导航栏字体颜⾊
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:17],NSFontAttributeName, nil]];
 
四、三⼤控制器的综合使⽤
 
    • UITabBarController、UINavigationController、 UITableViewController通常都是组合出现,这种布局⽅式 特别常见,⼀定要熟练应⽤。 
    • UITabBarController可以嵌套UINavigationController
    • UITabBarController也可以模态UINavigationController。
原文地址:https://www.cnblogs.com/Walking-Jin/p/5210878.html