IOS之UITabBarController
在学习IOS开发过程中,针对于UITabBarController的使用也不少出现,UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换
使用步骤:
1初始化UITabBarController
2设置UIWindow的rootViewController为UITabBarController
3创建相应的子控制器(viewcontroller)
4把子控制器添加到UITabBarController
代码如下
//初始化视图控制器UIViewController * vc1=[[UIViewController alloc] init];vc1.view.backgroundColor=[UIColor redColor];UIViewController * vc2=[[UIViewController alloc] init];vc2.view.backgroundColor=[UIColor greenColor];UIViewController * vc3=[[UIViewController alloc] init];vc3.view.backgroundColor=[UIColor yellowColor];UIViewController * vc4=[[UIViewController alloc] init];vc4.view.backgroundColor=[UIColor orangeColor];UIViewController * vc5=[[UIViewController alloc] init];vc5.view.backgroundColor=[UIColor purpleColor];//为tabbarController添加控制器UITabBarController * tabVC=[[UITabBarController alloc] init];tabVC.delegate=self;tabVC.viewControllers=@[vc1,vc2,vc3,vc4,vc5 ];//初始化系统UITabBarItemUITabBarItem * item1=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:101];vc1.tabBarItem=item1;UITabBarItem * item2=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:102];vc2.tabBarItem=item2;//初始化带图片的UITabBarItemUIImage * selImage=[UIImage imageNamed:@"tabbar_cate_f"];selImage=[selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];UITabBarItem * item3=[[UITabBarItem alloc] initWithTitle:@"最近" image:[UIImage imageNamed:@"tabbar_cate"] selectedImage:selImage];NSDictionary * dic=@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor redColor]};[item3 setTitleTextAttributes:dic forState:UIControlStateSelected];vc3.tabBarItem=item3;//初始化带图片的UITabBarItemUITabBarItem * item4=[[UITabBarItem alloc] initWithTitle:@"你好" image:[UIImage imageNamed:@"tabbar_fov"] tag:104];vc4.tabBarItem=item4;UITabBarItem * item5=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:105];vc5.tabBarItem=item5;//将TabBarController设置为窗口的根控制器 self.window.rootViewController=tabVC;
2.UITabBar自己有一些方法是可以改变自身状态,但是对于UITabBarController自带的tabBar还是不能满足需求的,因此们需要用到自定义
思路如下: 我们需要自定义一个tabbar,这里我们用UIView来替代,同时我们需要在tabbar上面来增加Item,从而达到点击控制视图控制器的目的。Item涉及到点击事件因此我们可以考虑用Button来完成,同时在button上面添加图标和标题。
1 我们自定义类JRTabBarController.h
2 分别实现三个方法
代码如下
#pragma mark - loadVC - (void) _loadVC{self.tabBar.hidden=YES;//1 创建视图控制器JRViewController * vc1=[[JRViewController alloc] init];UIViewController * vc2=[[UIViewController alloc] init];vc2.view.backgroundColor=[UIColor greenColor];UIViewController * vc3=[[UIViewController alloc] init];vc3.view.backgroundColor=[UIColor yellowColor];UIViewController * vc4=[[UIViewController alloc] init];vc4.view.backgroundColor=[UIColor blueColor];self.viewControllers=@[vc1,vc2,vc3,vc4];}#pragma mark - _makeTabBar - (void)_makeTabBar{//1 >定制tabbarUIView * bgview=[[UIView alloc] initWithFrame:CGRectMake(0, kHeight-49, kWidth, 49)];bgview.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];[self.view addSubview:bgview];//2 >定制btnCGFloat space=(kWidth-2*kLeftSpace-5*kBtSize)/4.0+kBtSize;for(int i=0;i<5;i++){NSDictionary * dic=_array[i];//1 初始化button大小JRButton * button= [[JRButton alloc] initWithFrame:CGRectMake(kLeftSpace+i*space, 49/2.0-kBtSize/2.0, kBtSize, kBtSize)];//2 初始化button的标题和图片[button initButtonWithTitle:dic[@"title"] andImage:dic[@"image"]];//3 为button 设置tag 和代理button.tag=i;button.delegate=self;[bgview addSubview:button];//4 将button 加入到数组,来调整选中背景的位置 [_btArray addObject:button];}//3 >增加选中图标_selectView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 49/2.0-(kBtSize+2)/2.0, kBtSize+4, kBtSize+4)];UIButton * button=_btArray[0];_selectView.center=button.center;_selectView.image=[UIImage imageNamed:@"bg"];[bgview addSubview:_selectView];[bgview sendSubviewToBack:_selectView];}#pragma mark - 加载数据 - (void) _loadData{_btArray=[NSMutableArray array];NSDictionary * dic1=@{@"title":@"电影",@"image":[UIImage imageNamed:@"movie_cinema"]};NSDictionary * dic2=@{@"title":@"新闻",@"image":[UIImage imageNamed:@"msg_new"]};NSDictionary * dic3=@{@"title":@"top",@"image":[UIImage imageNamed:@"star_top250"]};NSDictionary * dic4=@{@"title":@"影院",@"image":[UIImage imageNamed:@"icon_cinema"]};NSDictionary * dic5=@{@"title":@"更多",@"image":[UIImage imageNamed:@"more_select_setting"]};_array=@[dic1,dic2,dic3,dic4,dic5];}
2、这里还有一个需要完成的功能就是点击事件,当每个Item被点击的时候我们需要进行视图控制器的切换,在切换视图控制器的同时我们还需要控制黑色背景小图片的移动,下面我们来实现这个方法
#pragma mark - ButtonDelegate - (void)changePage:(NSInteger)index{//1 改变选中图片UIButton * button=_btArray[index];[UIView beginAnimations:nil context:nil];_selectView.center=button.center;[UIView commitAnimations];//2 切换视图控制器self.selectedIndex=index; }
3 、Item的定义,这里我们自定义一个button用来自定义Item,分别在button上面增加图片和标题来达到我们的效果,同时,通过代理实现控件的控制,代码如下:
/*** 初始化图片和标题** @param title 标题* @param image 图片*/ - (void) initButtonWithTitle:(NSString *) title andImage:(UIImage *) image{if(self) {//1> 添加ImageUIImageView * imageView=[[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width/2.0-kImageSize/2.0, 2, kImageSize, kImageSize)];imageView.contentMode=UIViewContentModeScaleAspectFit;imageView.image=image;[self addSubview:imageView];//2> 添加titleUILabel * label=[[UILabel alloc] initWithFrame:CGRectMake(0, kImageSize, self.frame.size.width, self.frame.size.height-kImageSize)];label.text=title;label.textColor=[UIColor whiteColor];label.textAlignment=NSTextAlignmentCenter;label.font=[UIFont boldSystemFontOfSize:13];[self addSubview:label];[self addTarget:self action:@selector(showClick) forControlEvents:UIControlEventTouchUpInside];}}
出处:http://www.cnblogs.com/jerehedu/
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询:
![](http://pub.idqqimg.com/wpa/images/group.png)
转载于:https://www.cnblogs.com/jerehedu/p/4884377.html
IOS之UITabBarController相关推荐
- ios更改UITabBarController背景以及选中背景图片的方法
不多说,直接上方案. 一.背景图片 1.5.0以上版本 UIImage *image = [UIImage imageNamed:@"system_tabbar_bg.pn ...
- ios swift UITabBarController present dismiss
如果实现了TabBarController的viewDidAppear方法,启动的时候,他选中的控制器实现的viewDidAppear方法不会被调用 present出的界面被dismiss,下面即将展 ...
- iOS开发UI篇—UITabBarController简单介绍
iOS开发UI篇-UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...
- iOS开发中标签控制器的使用——UITabBarController
iOS开发中标签控制器的使用--UITabBarController 一.引言 与导航控制器相类似,标签控制器也是用于管理视图控制器的一个UI控件,在其内部封装了一个标签栏,与导航不同的是,导航的管理 ...
- 从0开始架构一个IOS程序 ——04— UITabBarController和自定义TabBar 搭建主页面
从0开始架构一个IOS程序 04 UITabBarController 搭建主页面 Mac OSX 10.11 之后 效果 1 首先创建自定义TabBar 1.1 WISHomeTabBarView. ...
- iOS 怎么设置 UITabBarController 的第n个item为第一响应者?
iOS 怎么设置 UITabBarController 的第n个item为第一响应者? UITabBarController 里面有个属性:selectedIndex @property(nonato ...
- 【iOS】UIViewController、UINavigationController与UITabBarController的整合使用
原文 http://blog.csdn.net/rongxinhua/article/details/20214293 UINavigationController与UITabBarControll ...
- iOS 关于自定义转场动画,以UITabBarController为例
1.小记 关于自定义转场动画,只要你理清他的"套路",你就可以随心所欲地自定义了. 大体思路就是:遵守对应的代理协议,然后设置对应的代理,实现代理方法,这个代理方法要返回的值就是你 ...
- iOS:切换视图的第三种方式:UITabBarController标签栏控制器
UITabBarController:标签栏控制器 •通过设置viewControllers属性或者addChildViewController方法可以添加子控制器 –NSArray *viewCon ...
最新文章
- Spring Framework 5.3.6、5.2.14 发布
- python可以播放音乐吗_详解python播放音频的三种方法
- filezilla 设置服务器_java项目部署到linux服务器,微信小程序后台springboot项目部署到腾讯云服务器(图文详解)...
- 华为鸿蒙os生态,华为鸿蒙系统终于来了! 首款方舟编译器应用正式上架: 鸿蒙OS可用...
- android 资源如何下沉,关于Android业务模块下沉的一些实践及总结
- react 子传参父_React 子组件向父组件传值的方法
- DictionaryTKey,TValue
- 北大博士,毕业做北京城管,他的同事清一色名校硕士,博士,这个世界怎么了?...
- LeetCode刷题(28)
- 数学建模——遗传算法(GA)
- endnote导入参考文献及国标(Chinese standard)
- Cannot modify header information - headers already sent by (output started at 问题
- Proteus 8.6软件安装教程
- 2017年6月Mac OS 10.11安装文档
- 一个古老故事--线程和线程池的故事
- 域名停靠(域名抢注的几种形式)
- http://blog.sina.com.cn/s/blog_a3d2fd2d0101hyu7.html
- 用户运营中,怎么做好用户增长?
- CentOS7,Ubuntu系统中NCL的gedit编译器安装方法,亲测有效
- 商业数据分析师——IT和商业之间的纽带