0122——UITabBarController

时间:2022-06-10 04:07:49

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

0122——UITabBarController

一.创建

最常见的创建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;

}

0122——UITabBarController

二.tabbar

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

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

0122——UITabBarController

三.UITabBarButton

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

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

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

0122——UITabBarController

第一步:自己定义一个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. ASP.NET访问网络映射盘&实现文件上传读取功能

    最近在改Web的时候,遇到一个问题,要跨机器访问共享文件夹,以实现文件正常上传下载功能. 要实现该功能,可以采用HTTP的方式,也可以使用网络映射磁盘的方式,今天主要给大家分享一下使用网络映射磁盘的方 ...

  2. 谈谈Git的忽略规则.gitignore

    对于经常使用Git的朋友来说,.gitignore配置一定不会陌生. 今天就来说说这个.gitignore的使用. 首先要强调一点,这个文件的完整文件名就是“.gitignore”,注意最前面有个“. ...

  3. [译] Web API 之 简介

    事实上,MVC 框架本身已经提供了构建REST风格服务的基础,而Web API 只是让你可以更加容易和快捷的构建REST服务. 特性 基于约定的 CRUD Actions: 自动按照HTTP的acto ...

  4. 寒假D1 B

    B - B Crawling in process... Crawling failed Time Limit:0MS     Memory Limit:0KB     64bit IO Format ...

  5. POJ1008

    2014-08-22 题意: (有中文版题目..)  就是两种历法的转换 思路: 把两种历法的细节了解了就很简单了 Haab历法一年365,Tzolkin一年260天 先求出总天数sumDay,然后s ...

  6. MVC定义路由

    标准路由配置 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Defa ...

  7. Unity3d 网络编程(二)(Unity3d内建网络各项參数介绍)

    这里是全部Unity3d在网络中能用到相关的类及方法.纵观參数功能, Unity3d来写一个手游是不二的选择: RPC 能够传递的參数 int float string NetworkPlayer N ...

  8. 网络编程中select模型和poll模型学习(linux)

    一.概述 并发的网络编程中不管是阻塞式IO还是非阻塞式IO,都不能很好的解决同时处理多个socket的问题.操作系统提供了复用IO模型:select和poll,帮助我们解决了这个问题.这两个函数都能够 ...

  9. elasticsearch+logstash_jdbc 实现mysql数据实时同步至es

    jdk安装1.8版本,es.ls.ik.kibana版本一致我这里使用的6.6.2版本 安装es tar xf elasticsearch-6.6.2.tar.gz mv elasticsearch- ...

  10. mysql中文查询问题

    alter table t_foo change `str` `str` varchar(100) character set utf8 not null ;