iOSDay28之UITabBarController
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOSDay28之UITabBarController,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4947字,纯文字阅读大概需要8分钟。
内容图文
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]; 1112// 第二种方式:自定义样式13 UIViewController *secondVC = [[UIViewController alloc] init]; 1415 secondVC.view.backgroundColor = [UIColor redColor]; 1617// 创建图片18 UIImage *secondImage = [UIImage imageNamed:@"carGary"]; 1920 UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"]; 2122#pragma mark - 设置图片保留原有样式 23 secondImage = [secondImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 24 secondSelectImage = [secondSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 2526#pragma mark - 27 secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二页" image:secondImage selectedImage:secondSelectImage]; 2829// thirdVC30 UIViewController *thirdVC = [[UIViewController alloc] init]; 3132 thirdVC.view.backgroundColor = [UIColor purpleColor]; 3334 thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"findGray"] tag:101]; 3536// fourthVC37 UIViewController *fourthVC = [[UIViewController alloc] init]; 3839 fourthVC.view.backgroundColor = [UIColor greenColor]; 4041 fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"userGray"] tag:102]; 4243// fifthVC44 UIViewController *fifthVC = [[UIViewController alloc] init]; 4546 fifthVC.view.backgroundColor = [UIColor orangeColor]; 4748 fifthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:103]; 4950// sixthVC51 UIViewController *sixthVC = [[UIViewController alloc] init]; 5253 sixthVC.view.backgroundColor = [UIColor magentaColor]; 5455 sixthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:104]; 5657// 设置控制器数组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"]; 34 UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"]; 56#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]];
原文:http://www.cnblogs.com/gfxxbk/p/5403260.html
内容总结
以上是互联网集市为您收集整理的iOSDay28之UITabBarController全部内容,希望文章能够帮你解决iOSDay28之UITabBarController所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。