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. SVN的初步使用方法

    1.需要公司提供SVN账号密码 2.cd 输入本地存储路径 3.输入服务器地址 --uesr= (账号名) --password=(账号密码) 4.本地路径会自动创建文件 5.经理会初始化项目 5.1 ...

  2. Spring中的WebDataBinder浅析

    Spring可以自动封装Bean,也就是说可以前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等).如果传递过来 ...

  3. OpenGL ES 2.0 绘制方式

    OpenGL ES 中支持的绘制方式大致分3类,包括点.线段.三角形,每类中包括一种或多种具体的绘制方式. GL_POINTS 传入渲染管线的一系列顶点单独进行绘制. GL_LINES   传入渲染管 ...

  4. javaWeb--jsp & jQuery

    jsp页面的基本构成:指令标签HTML标记语言注释   <!-- html注释 -->  <%-- java代码注释 -->   //html注释对jsp嵌入的代码不起作用,因 ...

  5. c# 中的线程和同步

    一.新建线程的3种方法 a)异步委托:b)Thread类:c)线程池: 二.异步委托 1.简单使用,检查委托是否完成其任务 a) 通过 BeginInvoke()  的返回值IAsyncResult ...

  6. php 实时推送代码

    网站质量不错的网站可以在百度站长平台/数据提交/sitemap栏目下看到实时推送的功能, 目前这个工具是邀请开放, 百度的实时推送的api接口可以实时推送我们新发布的文章, 保证百度在第一时间收录. ...

  7. BZOJ1013 球形空间产生器sphere

    Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁 ...

  8. 03_Elasticsearch如何安装以及相关插件的介绍

    03_Elasticsearch如何安装以及相关插件的介绍 elasticsearch -d (-d参数是为了让服务后台运行) Elasticsearch 目录结构: 文件夹 作用 /bin 运行El ...

  9. flex渐变色制作圆角橙色按钮

    <?xml version="1.0" encoding="utf-8"?> <s:SparkButtonSkin xmlns:fx=&quo ...

  10. Hive 10、Hive的UDF、UDAF、UDTF

    Hive自定义函数包括三种UDF.UDAF.UDTF UDF(User-Defined-Function) 一进一出 UDAF(User- Defined Aggregation Funcation) ...