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对象

   UITabBarController  *mainTabBar = [[UITabBarController alloc] init];

     // 创建控制器对象
UIViewController *firstVC = [[UIViewController alloc] init]; firstVC.view.backgroundColor = [UIColor cyanColor]; // 设置tabBarItem
// 第一种方式:系统样式
firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:]; // 第二种方式:自定义样式
UIViewController *secondVC = [[UIViewController alloc] init]; secondVC.view.backgroundColor = [UIColor redColor]; // 创建图片
UIImage *secondImage = [UIImage imageNamed:@"carGary"]; UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"]; #pragma mark - 设置图片保留原有样式
secondImage = [secondImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
secondSelectImage = [secondSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; #pragma mark -
secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二页" image:secondImage selectedImage:secondSelectImage]; // thirdVC
UIViewController *thirdVC = [[UIViewController alloc] init]; thirdVC.view.backgroundColor = [UIColor purpleColor]; thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"findGray"] tag:]; // fourthVC
UIViewController *fourthVC = [[UIViewController alloc] init]; fourthVC.view.backgroundColor = [UIColor greenColor]; fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"userGray"] tag:]; // fifthVC
UIViewController *fifthVC = [[UIViewController alloc] init]; fifthVC.view.backgroundColor = [UIColor orangeColor]; fifthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:]; // sixthVC
UIViewController *sixthVC = [[UIViewController alloc] init]; sixthVC.view.backgroundColor = [UIColor magentaColor]; sixthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:]; // 设置控制器数组
mainTabBar.viewControllers = @[firstVC, secondVC, thirdVC];

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

self.window.rootViewController = mainTabBar;

 4> UITabBarController的重要属性

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

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

2. UITabBar

 1> 概述

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

  UITabBar 的高度是

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

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

 2> UItabBar常用的属性

     // tabBar的属性

     // 设置选中的颜色
mainTabBar.tabBar.tintColor = [UIColor greenColor]; // 是否打开半透明效果
mainTabBar.tabBar.translucent = NO; // 设置tabBar的颜色
// mainTabBar.tabBar.barTintColor = [UIColor grayColor];

 3> UITabBarItem

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

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

  ① 系统样式

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

  ② 自定义样式

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

  secondImage 和 secondSelectImage 是两个 UIImage 类型的变量

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

3. 自定义tabBar外观(UIAppearance)

 1> 概述

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

 2> 使用代码

     // 设置全局外观
// 通过[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:], NSFontAttributeName, nil]];

iOS学习28之UITabBarController的更多相关文章

  1. iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建)

    iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建)   一.UITabBarController在storyoard中得搭建 1.新建一个项目,把storyb ...

  2. iOS学习资料整理

    视频教程(英文) 视频 简介 Developing iOS 7 Apps for iPhone and iPad 斯坦福开放教程之一, 课程主要讲解了一些 iOS 开发工具和 API 以及 iOS S ...

  3. iOS 学习

    iOS 学习资料 (适合初学者) 本文资料来源于GitHub 一.视频教程(英文) Developing iOS 7 Apps for iPhone and iPad斯坦福开放教程之一, 课程主要讲解 ...

  4. iOS 学习资料汇总

    (适合初学者入门) 本文资料来源于GitHub 一.视频教程(英文) Developing iOS 7 Apps for iPhone and iPad斯坦福开放教程之一, 课程主要讲解了一些 iOS ...

  5. 2015最新iOS学习线路图

    iOS是由苹果公司开发的移动操作系统,以xcode为主要开发工具,具有简单易用的界面.令人惊叹的功能,以及超强的稳定性,已经成为iPhone.iPad 和iPod touch 的强大基础:iOS 内置 ...

  6. ios 学习路线总结

    学习方法 面对有难度的功能,不要忙着拒绝,而是挑战一下,学习更多知识. 尽量独立解决问题,而不是在遇到问题的第一想法是找人. 多学习别人开源的第三方库,能够开源的库一定有值得学习的地方,多去看别的大神 ...

  7. IOS学习笔记48--一些常见的IOS知识点+面试题

      IOS学习笔记48--一些常见的IOS知识点+面试题   1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...

  8. iOS学习之第二个View使用UITabBarViewController

    前面有一篇博文iOS学习之Tab Bar的使用和视图切换 这是在AppDelegate里使用Tabbar,这样的程序打开就是TabbarView了,有时候我们需要给程序做一些帮助页面,或者登录页面,之 ...

  9. iOS学习笔记-自己动手写RESideMenu

    代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...

随机推荐

  1. Linux(Ubuntu)之设定开机自启动

    分两种:对自建脚本,对已安装服务 对已安装服务, 只需在/etc/rc.local中添加相应的启动命令,如: 重启后,即可自启动服务. 对自建脚本自启动: 创建脚本test,修改其权限为755,如: ...

  2. python简介

    python起源 作者Guido van Rossum,荷兰人 在创建python之初,1989年12月份,Guido只是想用编程来打发圣诞的闲暇时光.Guido也希望能有一门语言既能够像C语言那样, ...

  3. 微信支付 - V3支付问题

    参考资料:http://www.2cto.com/weixin/201506/407690.html   1.微信公众号支付出错: 当前页面的URL未注册: get_brand_wcpay_reque ...

  4. HTTP 请求头中的 X-Forwarded-For

    https://imququ.com/post/x-forwarded-for-header-in-http.html

  5. jQuery插件:跨浏览器复制jQuery-zclip(转载)

    转载地址:http://www.cnblogs.com/linjiqin/p/3532451.html jQuery-zclip是一个复制内容到剪贴板的jQuery插件,使用它我们不用考虑不同浏览器和 ...

  6. Android之Picasso --zz

    简介: Picasso是Square公司开源的一个Android图形缓存库.可以实现图片下载和缓存功能. 特点: 1.加载载网络或本地图片并自动缓存处理: 2.链式调用: 3.图形转换操作,如变换大小 ...

  7. Shell编程基础教程5--文本过滤、正则表达式、相关命令

    5.文本过滤.正则表达式.相关命令    5.1.正则表达式(什么是正则表达式?正则表达式怎么进行匹配?常用命令)        简介:            一种用来描述文本模式的特殊语法      ...

  8. 如何在java中使用别人提供的jar包进行导入,编译,运行

    一步一步往前走, 现在折分! JAR包即为上篇文章的东东. 测试JAVA文件. package com.security; import com.security.AESencrp; /** * 实现 ...

  9. css 妙味 总结

    技巧一: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  10. php获取文件夹下面的文件列表和文件夹列表

    function getDir($dir) { $dirArray[] = NULL; if (false != ($handle = opendir( $dir ))) { $i=0; while ...