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

  系统最多只显示 5 个 UITabBarItem , 当 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];

  secondImage 和 secondSelectImage 是两个 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]];
时间: 2024-08-23 12:50:07

iOSDay28之UITabBarController的相关文章

Snail—UI学习之自定义标签栏UITabBarController

这里的背景跟上面的差不多 不过这里要用到AppDelegate的单例进行传值 首先到AppDelegate.h文件中 <span style="color:#FF0000;">#import <UIKit/UIKit.h> @interface WJJRootViewController : UITabBarController //声明一UIButton属性 来记录当前按下的按钮 @property (nonatomic,strong) UIButton *

iOS开发-UI (十一) UITabBarController

知识点: 1.UITabBarController使用 2.UITabBarItem使用 关于TabBarController除了本次整理的内容,有兴趣的可以看下我以前发过的这两篇,在实际开发中很实用的东西. RDVTabBarController的基本使用 以及tabbar的防止双点击方法 从tabBarController的一个item上的控制器跳转到另一个item上的控制器 ======================= UITabBarController 1.创建方式 2.如何把一个U

UITabBarController的创建与自定义TarBar---学习笔记三

代码如下: #import <UIKit/UIKit.h> @interface BSJTabBarViewController : UITabBarController @end #import "BSJTabBarViewController.h" #import "BSJTabBar.h" @interface BSJTabBarViewController () @end @implementation BSJTabBarViewControll

UITabBarController 基本定制

UITabBarController 定制 特点 用法 1.准备好你的tabBar图片及其他图片(哈哈哈!!!!),我的图片都放在了Assets.xcassets中. 2.导入本工程中的Categroy文件夹, 其中包含: HexColor.h/.m(设置颜色的), NSString+RenderingModel.h/.m(处理图片,让其保持本色或者默认的那种), UIColor+CreateImage.h/.m(填充tabBar的背景颜色), UIFont+fonts.h/.m(设置字体),

UITabBarController未呈现时present另一个ViewController会发生什么?

一次给了下面两条警告(精彩吧): Presenting view controllers on detached view controllers is discouraged Unbalanced calls to begin/end appearance transitions for <UITabBarController: 0x7fc046131d70>. 解决办法,dispatch_after 延迟present就好了.

UITabBarController简单介绍

转自:http://www.cnblogs.com/wendingding/p/3775488.html 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ.微信等应?. 二.UITabBarController的使用 1.使用步骤: (1)初始化UITabBarController (2)设置UIWindow的rootViewContr

UITabBarController的创建等基本方法

1 #import "AppDelegate.h" 2 3 @interface AppDelegate () <UITabBarControllerDelegate> 4 5 @end 6 7 @implementation AppDelegate 8 9 10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptio

iOS基础之UITabBarController(标签视图控制器)

UITabBarController是可以帮我们添加.管理许多的标签项,使我们的程序包含不同的操作模式,由于管理UITabBar可能比较麻烦,系统帮我们对其进行了封装,产生了简单好用的UITabBarController--标签视图控制器. 代码演示: #import "AppDelegate.h" #import "FirstViewController.h" #import "SecondViewController.h" #import &

2016.01.21 UITabBarController

UITabBarController是IOS中很常用的一个viewController.UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的container viewController中.可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ.微信.微博等应?. ?.创建 在storyboard中的模拟我们就不多说了,直接进入直接代码的编写.(模拟微博的tabBarController进行编写) 我们选择在Applic