UITabBarController是IOS中很常用的一个viewController。UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的container viewController中.

一.创建

最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLaunching:方法,因为UITabBarController通常是作为整个程序的rootViewController的,我们需要在程序的window显示之前就创建好它,具体步骤如下:

  1、创建一个UITabBarController对象

  2、创建tabbarcontroller中每一个tab对应的要显示的对象子控制器

  3、通过设置UITabBarController对象为window.rootViewController,然后显示window

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//创建窗口

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

//创建tabbarcontroller

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

tabbarController.tabBar.tintColor = [UIColor orangeColor]; //显示的字体颜色

//创建每个界面

HomeViewController * homeVC =[[HomeViewController alloc]init];

homeVC.view.backgroundColor =[UIColor whiteColor];

homeVC.tabBarItem = [[UITabBarItem alloc]initWithTitle: @"主页"image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[[UIImage imageNamed:@"tabbar_home_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];//使用自身的颜色,而不是默认颜色

//将创建的添加到tabbarController

tabbarController.viewControllers = @[homeVC,messVC,discVC,myVC];//加的顺序就是现实的顺序

self.window.rootViewController = tabbarController;

//显示窗口

[self.window makeKeyAndVisible];

return YES;

}

二.tabbar

屏幕下方的工具条称为UITabBar ,如果UITabBarController有N个子控制器,那么UITabBar内部就会有N 个UITabBarButton作为子控件与之对应。

注意:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度为49。

三.UITabBarButton

UITabBarButton⾥面显⽰什么内容,由对应子控制器的tabBarItem属性来决定

homeVC.tabBarItem.title=@"消息";

四.在tabbar中间加入一个按钮

第一步:自己定义一个CustomTabBarController,一个CustomUITabBar

第二步:CustomUITabBar中重新布局(给button让出一个位置)

- (void)layoutSubviews{

[super layoutSubviews];

//计算每个item的平均宽度

CGFloat avgWith = self.frame.size.width/5;

NSInteger index = 0;

for (UIView *item in self.subviews) {

if ([item isKindOfClass:NSClassFromString(@"UITabBarButton")]) {

item.frame = CGRectMake(index * avgWith, item.frame.origin.y, avgWith, item.frame.size.height);

index ++;

if (index == 2) {

_addButton.frame = CGRectMake(index * avgWith, 3, avgWith, 44);

[self addSubview:_addButton];//加入button

index++;

}

}

}

}

CustomTabBarController和CustomUITabBar之间是代理关系,而tabbar是只读属性,用下面的话来设置

@implementation CustomTabBarController

- (void)viewDidLoad {

[super viewDidLoad];

CustomUITabBar *tb = [[CustomUITabBar alloc]init];

tb.delegate = self;

[self setValue:tb forKey:@"tabBar"];//只读,属性设置不了的时候

}

第三步:实现代理

代理实现点击button打开页面

– presentViewController:animated:completion: 弹出一个新视图 可以带动画效果,完成后可以做相应的执行函数经常为nil
– dismissViewControllerAnimated:completion:退出一个新视图 可以带动画效果,完成后可以做相应的执行函数经常为nil

-(void)addButtonDidClicked:(UIButton *)sender{

addViewController * addVC = [[addViewController alloc]init];

addVC.view.backgroundColor = [UIColor whiteColor];

[self presentViewController:addVC animated:YES completion:nil];

}

0122——UITabBarController的更多相关文章

  1. UITabBarController 升级定制

    UITabBarController 定制 特点 用法 1.准备工作: 加入你的相关图片,放入了Assets.xcassets; 导入Categroy文件夹(这个里面的文件,在这里不详细说明了,有疑问 ...

  2. UITabBarController 基本定制

    UITabBarController 定制 特点 用法 1.准备好你的tabBar图片及其他图片(哈哈哈!!!!),我的图片都放在了Assets.xcassets中. 2.导入本工程中的Categro ...

  3. 自定义UITabBarController标签视图控制器

    首先创建一个类,继承自UItabBarController 然后在.m文件中: 这里我有两个宏定义: #define WIDTH (myView.frame.size.width / 4) //我在写 ...

  4. 混合使用UITabBarController和UINavigationController

    混合使用这两个控件的好处是我们可以在NavigationBar添加更多的东西,如标题,按钮等.让用户能够获得更多的信息. UITabBarController的属性ViewControllers接受以 ...

  5. 基本组件的使用——UITabBarController

    和UINavigationController的作用差不多,UITabBarController也可以在多个UIViewController中切换 这个控件的使用相对简单,只需要为该控件的viewCo ...

  6. Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)

    原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...

  7. iOS 怎么设置 UITabBarController 的第n个item为第一响应者?

    iOS 怎么设置 UITabBarController 的第n个item为第一响应者? UITabBarController 里面有个属性:selectedIndex @property(nonato ...

  8. iOS UITabBarController的使用

    UITabBarController 和 UINavigationController 几乎是iOS APP的标配. UITabBarController分栏(标签栏)控制器, 和UINavigati ...

  9. iOS开发UI篇—UITabBarController简单介绍

    iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...

随机推荐

  1. .net 将xml转换成DateSet

    /// <summary> /// 将XML字符串转换成DATASET /// </summary> /// <param name="xmlStr" ...

  2. 清空文本框SetDlgItemText(IDC_TXTXT,NULL);

    清空文本框 SetDlgItemText(IDC_TXTXT,NULL);

  3. shell中的双括号表达式

    语法格式 (( expression )) expression可以是任何数学表达式,可以包含的操作符有: +  加 - 减 * 乘(无需转义) / 除 % 取余 ** 指数 == 等于 != 不等 ...

  4. 用document.onreadystatechange和document.readyState确保文档加载完毕才获取DOM

    document.onreadystatechange = function(){ //document.readyState有“interactive”和“complate”两个值 if(docum ...

  5. [Apache系列]怎样在windows下配置apache vhost

    找到你的Apache安装目录,下图为小编的Apache安装的目录 2 点击conf文件夹  进入配置目录,找到httpd.conf 文件, 3 打开httpd.conf 文件,如图, 找到地475行, ...

  6. 如何让你的 footer 总是在浏览器底部无论什么分辨率无论什么浏览器?

    一个可以用的CSS驱动的可粘在底部的Footer 我们曾经都或多或少的试过用CSS来把Footer固定在底部的经理,但是他们总是不能正常的粘在底部,不是吗?可喜的是,痛苦的研究如何让footer粘在底 ...

  7. Monkey and Banana(HDU 1069 动态规划)

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. 转载----给JavaScript初学者的24条最佳实践

      给JavaScript初学者的24条最佳实践 1.使用 === 代替 == JavaScript 使用2种不同的等值运算符:===|!== 和 ==|!=,在比较操作中使用前者是最佳实践. “如果 ...

  9. 正则匹配<img src="xxxxxx" alt="" />标签的相关写法

    1.(<img\ssrc[^>]*>) 2.content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*&g ...

  10. ORA-07217: sltln: environment variable cannot be evaluated及RMAN-06059

    备份脚本: RMAN> run { allocate channel c1 device type disk format '$BACKUP_HOME/level0/level0_%d_%s_% ...