iOSDay28之UITabBarController

1. 标签视图控制器 -- UITabBarController 
  • 视图(UIView) ---> 图层 ---> 子视图
  • 视图控制器(UIViewController) ---> 管理视图
  • 导航控制器(UINavigationController) ---> 管理有层次关系的视图控制器
  • 标签视图控制器(UITabBarController) ---> 管理没有层次关系的视图控制器

 1> UITabBarController的继承关系

  @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>

 2> UITabBarController的三层结构

 3> 代码创建UITabBarController

  在application: idFinishLaunchingWithOptions:方法中创建

  ① 创建Window(需要将工程的主故事版删除)

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self.window makeKeyAndVisible];

  ② 创建UITabBarController对象

 1   UITabBarController  *mainTabBar = [[UITabBarController alloc] init];
 2     
 3     // 创建控制器对象
 4     UIViewController *firstVC = [[UIViewController alloc] init];
 5     
 6     firstVC.view.backgroundColor = [UIColor cyanColor];
 7     
 8     // 设置tabBarItem
 9     // 第一种方式:系统样式
10     firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100];
11     
12     // 第二种方式:自定义样式
13     UIViewController *secondVC = [[UIViewController alloc] init];
14     
15     secondVC.view.backgroundColor = [UIColor redColor];
16     
17     // 创建图片
18     UIImage *secondImage = [UIImage imageNamed:@"carGary"];
19     
20     UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"];
21     
22 #pragma mark - 设置图片保留原有样式
23     secondImage = [secondImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
24     secondSelectImage = [secondSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
25     
26 #pragma mark -
27     secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二页" image:secondImage selectedImage:secondSelectImage];
28     
29     // thirdVC
30     UIViewController *thirdVC = [[UIViewController alloc] init];
31     
32     thirdVC.view.backgroundColor = [UIColor purpleColor];
33     
34     thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"findGray"] tag:101];
35     
36     // fourthVC
37     UIViewController *fourthVC = [[UIViewController alloc] init];
38     
39     fourthVC.view.backgroundColor = [UIColor greenColor];
40     
41     fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"userGray"] tag:102];
42     
43     // fifthVC
44     UIViewController *fifthVC = [[UIViewController alloc] init];
45     
46     fifthVC.view.backgroundColor = [UIColor orangeColor];
47     
48     fifthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:103];
49     
50     // sixthVC
51     UIViewController *sixthVC = [[UIViewController alloc] init];
52     
53     sixthVC.view.backgroundColor = [UIColor magentaColor];
54     
55     sixthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:104];
56     
57     // 设置控制器数组
58     mainTabBar.viewControllers = @[firstVC, secondVC, thirdVC];

  ③ 将UITabBarController对象设置为Window的根视图控制器

   1 self.window.rootViewController = mainTabBar;                                                 

 4> UITabBarController的重要属性

  viewControllers属性的应用件 3> ② 的代码

  // 设置进入应用时选中第几个
    mainTabBar.selectedIndex = 1;

2. UITabBar 

 1> 概述

  UITabBar 包含多个 UITabBarItem , 每个 UITabBarItem 对应一个 UIViewController

  UITabBar 的高度是 49

  系统最多只显示 5UITabBarItem , 当 UITabBarItem 超过 5 个时系统会自动增加一个更多按钮, 点击更多按钮, 没有在底部出现的按钮会以 列表 的形式显示出来

  UITabBar的属性: tintColor , barTintColor , 图像设置

 2> UItabBar常用的属性

 1     // tabBar的属性
 2     
 3     // 设置选中的颜色
 4     mainTabBar.tabBar.tintColor = [UIColor greenColor];
 5     
 6     // 是否打开半透明效果
 7     mainTabBar.tabBar.translucent = NO;
 8     
 9     // 设置tabBar的颜色
10 //    mainTabBar.tabBar.barTintColor = [UIColor grayColor];

 3> UITabBarItem

  •  UITabBarItem 可以通过属性 title , badgeValue 设置标题及提示  

  // 设置提示
    thirdVC.tabBarItem.badgeValue = @"有消息";
  • UITabBarItem 的创建

  ① 系统样式

    // 第一种方式:系统样式
    firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100];

  ② 自定义样式

// 第二种方式:自定义样式
secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二页" image:secondImage selectedImage:secondSelectImage];

  secondImagesecondSelectImage 是两个 UIImage 类型的变量

  • UITabBarItem 的图片处理
1     // 创建图片
2     UIImage *secondImage = [UIImage imageNamed:@"carGary"];
3     
4     UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"];
5     
6 #pragma mark - 设置图片保留原有样式
7     secondImage = [secondImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
8     secondSelectImage = [secondSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

3. 自定义tabBar外观(UIAppearance)

 1> 概述

  如果想通过一键设定所有导航试图控制器的颜色, 类似于QQ的一键换肤操作,可以通过UIAppearance 协议 来进行操作, 通过它可以对一些控件进行定义颜色等。

 2> 使用代码

1     // 设置全局外观
2     // 通过[UITabBar appearance]得到当前应用的UITabBar对象来设置tabBar的外观
3     // 注意:设置全局外观最好在appDelegate ,否则会无效
4     [[UITabBar appearance] setBarTintColor:[UIColor cyanColor]]; [[UITabBar appearance] setTintColor:[UIColor brownColor]];
5     // 改变导航栏外观颜
6     [[UINavigationBar appearance] setBarTintColor:[UIColor lightGrayColor]];
7     // 改变导航栏字体颜
8     [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName, [UIFont systemFontOfSize:17], NSFontAttributeName, nil]];

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值