在学习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 ]; //初始化系统UITabBarItem
UITabBarItem * item1=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:];
vc1.tabBarItem=item1; UITabBarItem * item2=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:];
vc2.tabBarItem=item2; //初始化带图片的UITabBarItem
UIImage * 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:],NSForegroundColorAttributeName:[UIColor redColor]};
[item3 setTitleTextAttributes:dic forState:UIControlStateSelected];
vc3.tabBarItem=item3; //初始化带图片的UITabBarItem
UITabBarItem * item4=[[UITabBarItem alloc] initWithTitle:@"你好" image:[UIImage imageNamed:@"tabbar_fov"] tag:];
vc4.tabBarItem=item4; UITabBarItem * item5=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:];
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 >定制tabbar
UIView * bgview=[[UIView alloc] initWithFrame:CGRectMake(, kHeight-, kWidth, )];
bgview.backgroundColor=[UIColor colorWithRed: green: blue: alpha:0.5];
[self.view addSubview:bgview]; //2 >定制btn
CGFloat space=(kWidth-*kLeftSpace-*kBtSize)/4.0+kBtSize; for(int i=;i<;i++){ NSDictionary * dic=_array[i];
//1 初始化button大小
JRButton * button= [[JRButton alloc] initWithFrame:CGRectMake(kLeftSpace+i*space, /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(, /2.0-(kBtSize+)/2.0, kBtSize+, kBtSize+)];
UIButton * button=_btArray[];
_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> 添加Image
UIImageView * imageView=[[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width/2.0-kImageSize/2.0, , kImageSize, kImageSize)];
imageView.contentMode=UIViewContentModeScaleAspectFit;
imageView.image=image;
[self addSubview:imageView]; //2> 添加title
UILabel * label=[[UILabel alloc] initWithFrame:CGRectMake(, kImageSize, self.frame.size.width, self.frame.size.height-kImageSize)];
label.text=title;
label.textColor=[UIColor whiteColor];
label.textAlignment=NSTextAlignmentCenter;
label.font=[UIFont boldSystemFontOfSize:];
[self addSubview:label];
[self addTarget:self action:@selector(showClick) forControlEvents:UIControlEventTouchUpInside]; } }
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

技术咨询:
 

IOS之UITabBarController的更多相关文章

  1. iOS programming UITabBarController

    iOS programming UITabBarController 1.1 View controllers become more interesting when the user's acti ...

  2. iOS中 UITabBarController中自定义UITabBar

    1.创建多个视图控制器,放如UITabBarController中 AViewController *aa = [[AViewController alloc] init]; UINavigation ...

  3. iOS开发-UITabBarController详解

    我们在开发中经常会使用到UITabBarController来布局App应用,使用UITabBarController可以使应用看起来更加的清晰,iOS系统的闹钟程序,ipod程序都是非常好的说明和A ...

  4. IOS开发 UITabBarController

    UITabBarController使用详解 UITabBarController是IOS中很常用的一个viewController,例如系统的闹钟程 序,ipod程序等.UITabBarContro ...

  5. iOS 自定义UITabBarController的tabBar

               #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDeleg ...

  6. ios更改UITabBarController背景以及选中背景图片的方法

    一.背景图片  1.5.0以上版本     UIImage *image = [UIImage imageNamed:@"system_tabbar_bg.png"];     [ ...

  7. iPad编程

    1. iPad 现有型号: iPad Pro, iPad Air, iPad mini 均配备Retina显示屏.早期还有iPad 依次对应的坐标系及分辨率: iPad Pro 坐标系:1366 x ...

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

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

  9. iOS UITabBarController的使用

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

随机推荐

  1. <泛> 并查集

    最近写这些东西呢,主要是整理一下知识,自己写一遍,看看还是不是我的. 原理与实践相结合,缺一不可 背景 有时候,给你一张很复杂的关系网络图,如果关系具有传递性,那么,我们该如何处理这些关系集合. 一个 ...

  2. Python csv模块的使用

    1.csv简介 CSV (Comma Separated Values),即逗号分隔值(也称字符分隔值,因为分隔符可以不是逗号),是一种常用的文本 格式,用以存储表格数据,包括数字或者字符.很多程序在 ...

  3. JavaSE基础之封装

    JavaSE基础之封装 一.Java中的封装 1.字面意思: 包装: 2.专业含义: 面向对象的三大特征之一: 指的是将对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象内部信息,而是通过该类所 ...

  4. 【数论】Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!] C. Finite or not?

    题意:给你一个分数,问你在b进制下能否化成有限小数. 条件:p/q假如已是既约分数,那么如果q的质因数分解集合是b的子集,就可以化成有限小数,否则不能. 参见代码:反复从q中除去b和q的公因子部分,并 ...

  5. TASKER 手机在有同一个号码的三个未接电话时自动回复短信

    http://tieba.baidu.com/p/3695018030 感谢默默为Tasker吧奉献的人! 配置为>未接来电 任务为>代码>javascriptlet 代码为: va ...

  6. TLC2262和TLC2264 轨对轨运算放大器

    TLC2262和TLC2264分别是TI公司双路和四路运算放大器,两种器件可以在单电源或双电源条件下供电,从而增强了动态的范围,可以达到轨对轨输出的性能.TLC226X系列与TLC225X的微功耗和T ...

  7. LM358资料及引脚图

    LM358里面包括有两个高增益.独立的.内部频率补偿的双运放,适用于电压范围很宽的单电源,而且也适用于双电源工作方式,它的应用范围包括传感放大器.直流增益模块和其他所有可用单电源供电的使用运放的地方使 ...

  8. AWR--service statistics

    近期发现一个奇怪的现象,数据库报告上看负载非常高.可是cpu和等待事件都非常低,不知道消耗的资源跑到哪里去了? Snap Id Snap Time Sessions Cursors/Session B ...

  9. 32.NET中加密解密基本概念

    对消息的接收方来说,安全的交流方式需要同时满足3个条件: 1.完整性:消息在传输途中没有被篡改过,即消息是完好无损的. 2.保密性:接收放可以理解或解密来自发送方的信息.(不保证第三方无法获得,但保证 ...

  10. [转载] C-MEX程序编写

    原作者,胡荣春 2006-10-11 1  MEX文件简介 在MATLAB中可调用的C或Fortran语言程序称为MEX文件.MATLAB可以直接把MEX文件视为它的内建函数进行调用.MEX文件是动态 ...