UITabBarController 和 UINavigationController 几乎是iOS APP的标配。

UITabBarController分栏(标签栏)控制器, 和UINavigationController导航控制器一样,都是用来管理视图控制器的容器类型的控制器。

 1、UITabBarController的基本使用

(1)、创建几个视图控制器

(2)、将视图控制器添加到TabBarController

(3)、设置窗口的跟视图控制器为分栏控制器

(4)、设置分栏控制器的显示样式

    UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor redColor];
vc1.title = @"VC1"; // 视图控制器的分栏按钮
// 视图控制器的分栏按钮,如果没有被显示创建,并且被使用了,则会自动根据视图控制器的title来创建
//vc1.tabBarItem; UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor yellowColor];
vc2.title = @"VC2"; UIViewController *vc3 = [[UIViewController alloc] init];
vc3.view.backgroundColor = [UIColor blueColor];
vc3.title = @"VC3"; UIViewController *vc4 = [[UIViewController alloc] init];
vc4.view.backgroundColor = [UIColor greenColor];
vc4.title = @"VC4"; // 创建分栏(标签栏)控制器, 和导航控制器一样,都是用来管理视图控制器的容器类型的控制器。
// 分栏控制器和导航控制器一样,也是通过viewControllers来管理其子视图控制器
UITabBarController *tabBarCtrl = [[UITabBarController alloc] init]; tabBarCtrl.view.backgroundColor = [UIColor cyanColor]; // 把数据中得视图器交给分栏控制器管理
// 分栏控制器会自动将其管理的视图控制器的分栏按钮(UITabBarItem)放到分栏上显示
tabBarCtrl.viewControllers = @[vc1, vc2, vc3, vc4]; // 设置窗口的跟视图控制器为分栏控制器
self.window.rootViewController = tabBarCtrl; // 取到分栏控制器的分栏
UITabBar *tabBar = tabBarCtrl.tabBar; // 设置分栏的风格
tabBar.barStyle = UIBarStyleBlack; // 是否透明
tabBar.translucent = NO; // 设置分栏的前景颜色
tabBar.barTintColor = [UIColor brownColor]; // 设置分栏元素项的颜色
tabBar.tintColor = [UIColor purpleColor]; // 设置分栏按钮的选中指定图片
tabBar.selectionIndicatorImage = [UIImage imageNamed:@"home.png"]; [self.window makeKeyAndVisible];

效果如图:

 2、UITabBarController和的UINavigationController结合使用

如果有如图需求:点击切换到下一个页面,隐藏UITabBarController。

(1)、创建分栏控制器:

// 创建分栏控制器管理的子视图控制器
- (void)createViewControllers
{
OneViewController *oneVC = [[OneViewController alloc] init];
MyNavgationController *navCtrl1 = [[MyNavgationController alloc] initWithRootViewController:oneVC];
// navCtrl1.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0]; TwoViewController *twoVC = [[TwoViewController alloc] init];
UINavigationController *navCtrl2 = [[UINavigationController alloc] initWithRootViewController:twoVC]; ThreeViewController *threeVC = [[ThreeViewController alloc] init];
FourViewController *fourVC = [[FourViewController alloc] init]; FiveViewController *fiveVC = [[FiveViewController alloc] init]; SixViewController *sixVC = [[SixViewController alloc] init]; // 分栏控制器管理的视图控制器的tabBarController属性,自动指向分栏控制器。
// 当分栏控制器管理的视图控制器的个数超过五个时,会自动创建一个more的导航控制器,并且自动将第五个以及以后的视图控制器添加到more导航控制器中。
self.viewControllers = @[navCtrl1, navCtrl2, threeVC, fourVC, fiveVC, sixVC];
}

(2)、在MyNavgationController里面,重写跳转方法:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//1. 取出分栏
UITabBar *tabBar = self.tabBarController.tabBar; // 将frame左移分栏的宽度
CGRect frame = tabBar.frame;
frame.origin.x -= tabBar.frame.size.width; // 动画影藏tabBar
[UIView animateWithDuration:0.28 animations:^{
tabBar.frame = frame;
}]; [super pushViewController:viewController animated:animated];
} - (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
//1. 取出分栏
UITabBar *tabBar = self.tabBarController.tabBar; // 将frame左移分栏的宽度
CGRect frame = tabBar.frame;
frame.origin.x += tabBar.frame.size.width; // 动画影藏tabBar
[UIView animateWithDuration:0.28 animations:^{
tabBar.frame = frame;
}]; return [super popViewControllerAnimated:YES];
}

 3、定制UITabBarController

#import "MyTabBar.h"

@interface MyTabBarController () <MyTabBarDelegate>

// 保存之前选中的按钮
@property (nonatomic, retain) UIButton *preSelBtn;
@end @implementation MyTabBarController - (void)viewDidLoad {
[super viewDidLoad];
[self createViewControllers]; [self createTabBar];
} // 创建分栏控制器管理的视图控制器数据
- (void)createViewControllers
{
FirstViewController *firstVC = [[FirstViewController alloc] init];
UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:firstVC]; SecondViewController *secondVC = [[SecondViewController alloc] init]; ThirdViewController *thirdVC = [[ThirdViewController alloc] init]; FourthViewController *fourthVC = [[FourthViewController alloc] init]; self.viewControllers = @[nav1, secondVC, thirdVC, fourthVC];
} // 定制tabBar tabBar 49
- (void)createTabBar
{
// 0. 隐藏分栏控制器自带的tabbar
[self.tabBar setHidden:YES]; MyTabBar *tabBr = [[MyTabBar alloc] init]; tabBr.frame = self.tabBar.frame;
tabBr.delegate = self; [self.view addSubview:tabBr];
} #pragma mark - MyTabBarDelegate协议方法
- (void)tabBar:(MyTabBar *)tabBar didSelectItemWithIndex:(NSUInteger)toIndex
{
// 设置分栏控制器选中的视图控制器
self.selectedIndex = toIndex;
}
@class MyTabBar;

@protocol MyTabBarDelegate <NSObject>

@optional
- (void)tabBar:(MyTabBar *)tabBar didSelectItemWithIndex:(NSUInteger)toIndex; - (void)tabBar:(MyTabBar *)tabBar didSelectItemWithIndex:(NSUInteger)toIndex fromIndex:(NSUInteger)fromIndex; @end @interface MyTabBar : UIImageView @property (nonatomic, assign) id <MyTabBarDelegate> delegate; @end
#import "MyTabBar.h"

#define TABBAR_BTN_START_TAG 100
#define BTN_COUNT 4 @interface TabBarButton : UIButton @end @implementation TabBarButton // 重写高亮的方法。在这个方法中,我们忽略按钮的高亮状态
- (void)setHighlighted:(BOOL)highlighted
{
// NSLog(@"highlighted = %d", highlighted);
// [super setHighlighted:NO];
} @end @interface MyTabBar () // 保存分栏之前选中的按钮
@property (nonatomic, retain) TabBarButton *preSelBtn;
@end @implementation MyTabBar - (instancetype)init
{
if (self = [super init]) {
NSLog(@"init");
} return self;
} // init方法会调用initWithFrame ,给控件添加子控件在initWithFrame中添加
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) { // 1.2 设置背景视图的背景图片
self.image = [UIImage imageNamed:@"tabbg.png"]; // 1.3 设置用户交互属性可用(父控件不能与用户进行交互,则所有子控件都不能与用户交互)
self.userInteractionEnabled = YES; // 2. 在分栏背景上添加按钮
for (int i = ; i < BTN_COUNT; i++) {
TabBarButton *btn = [TabBarButton buttonWithType:UIButtonTypeCustom]; // 设置正常状态下的显示图片
NSString *imageName = [NSString stringWithFormat:@"tab_%d", i];
[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; // 设置按钮在选中状态下的显示图片
NSString *imageSelName = [NSString stringWithFormat:@"tab_c%d",i];
[btn setImage:[UIImage imageNamed:imageSelName] forState:UIControlStateSelected]; // 第一个按钮默认为选中
if (i == ) {
btn.selected = YES;
self.preSelBtn = btn;
} // 监听按钮的点击事件
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown]; btn.tag = TABBAR_BTN_START_TAG + i; // 将按钮添加到背景视图上显示
[self addSubview:btn];
}
} return self;
} - (void)btnClick:(TabBarButton *)btn
{
// 1. 取消之前选中按钮的选中状态
self.preSelBtn.selected = NO; // 2. 将当前点击的按钮设为选中状态
btn.selected = YES; // 3. 保存当前选中的按钮
self.preSelBtn = btn; // 如果代理实现了协议方法,通过协议方法通知代码当前控件上的第几个按钮被点击了。
if ([self.delegate respondsToSelector:@selector(tabBar:didSelectItemWithIndex:)]) { [self.delegate tabBar:self didSelectItemWithIndex:btn.tag - TABBAR_BTN_START_TAG];
}
} // 这个方法在控件的frame(大小)修改后被调用
- (void)layoutSubviews
{
// 这行代码不能少
[super layoutSubviews]; // 取出子控件的个数
NSUInteger count = self.subviews.count; // 调整子控件的大小
CGFloat btnY = ;
CGFloat btnW = self.bounds.size.width / count;
CGFloat btnH = self.bounds.size.height; int i = ;
// 取出所有的子控件,调整frame
for (TabBarButton *btn in self.subviews) {
// 设置frame
CGFloat btnX = btnW * i;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
i++;
}
}

效果如图:

iOS UITabBarController的使用的更多相关文章

  1. iOS - UITabBarController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate ...

  2. iOS - UITabBarController中的坑

    当你创建一个继承与UITabBarController的子类 并想给其自定义构造方法 传一些值的时候这时候问题出现了: 在创建的时候里面的init方法回调用了 viewdidload,导致每次传值的时 ...

  3. IOS UITabBarController(控制器)的子控制器

    UITabBarController的简单使用 ● UITabBarController的使用步骤 ➢ 初始化UITabBarController ➢ 设置UIWindow的rootViewContr ...

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

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

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

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

  6. IOS开发之路四(UITabBarController)

    前两天看了看斯坦福大学的iphone开发公开课,讲的倒是不错,可看的我云里雾里的,不怎么讲基础和原理,不太适合初学者.今天看了一上午ios5基础教程这本书感觉有点头绪了....废话少说,讲一讲我上午做 ...

  7. iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController)

    iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController)   前面我们介绍了StoryBoard这个新技术,和纯技术 ...

  8. iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)

    iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)   这里我们就直接上实例: 一:新建一个项目singleV ...

  9. iOS开发——实战OC篇&环境搭建之StoryBoard(玩转UINavigationController与UITabBarController)

      环境搭建之StoryBoard(玩转UINavigationController与UITabBarController)   研究了这么就IOS开发,都没有所处一个像样或者自己忙一点的项目.最近自 ...

随机推荐

  1. Owin的URL编码怎么搞?以前都是HttpUtility.UrlEncode之类的,现在连system.web都没了,肿么办?

    Owin的URL编码怎么搞?以前都是HttpUtility.UrlEncode之类的,现在连system.web都没了,肿么办? 编码: Uri.EscapeDataString(name) 解码: ...

  2. matlab 循环读入多个mat文件组合成一个mat文件

    今天做实验,需要到这个功能,在朋友的告知下,写了代码,在此留个标记 clc clear load('F:\效果对比\colorhist\1.mat'); a=ans; a=a'; : filename ...

  3. Hibernate的数据删除,更改

    其他未给出代码,请参考上一篇.... 一.数据的删除 方法1.从“多”的一方进行数据的删除 books.hbm.xml文件不变: <many-to-one name="publishe ...

  4. 浅谈async、await关键字 => 深谈async、await关键字

    前言 之前写过有关异步的文章,对这方面一直比较弱,感觉还是不太理解,于是会花点时间去好好学习这一块,我们由浅入深,文中若有叙述不稳妥之处,还请批评指正. 话题 (1)是不是将方法用async关键字标识 ...

  5. JavaScript 框架设计

    JavaScript 高级框架设计 在现在,jQuery等框架已经非常完美,以致于常常忽略了JavaScript原生开发,但是这是非常重要的. 所以,我打算写一个简单的框架,两个目的 熟练框架的思想 ...

  6. 微信扫码支付+Asp.Net MVC

    这里的扫码支付指的是PC网站上面使用微信支付,也就是官方的模式二,网站是Asp.net MVC,整理如下.(demo在最下方) 一.准备工作 使用的微信API中的统一下单方法,关键的参数是‘公众账号I ...

  7. jquery 图片轮播demo实现

    转载注明出处!!! 转载注明出处!!! 转载注明出处!!! 图片轮播demo,弄清楚过程其实是一个很简单的东西,看网上都没有什么实质性的代码,就自己把过程捋了一遍实现了. 这次因为随手写的,所以没有做 ...

  8. Android之实现ViewPagerIndicator

    PS:最近一直忙于学习任务,一直没有时间去写博客.今天周六,终于有时间了. 学习任务: 1.打造一个自己的ViewPagerIndicator   最近被安排了一大堆的学习任务,感觉老板还是很好的,让 ...

  9. Tomcat创建HTTPS访问,java访问https

    一 https和ssL HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的 ...

  10. 使用AutoMapper进行对象间映射

    在开发过程中,难免遇到下面这种情况:两个(或多个)对象所拥有的大多数属性是重复的,我们需要在对象间进行映射(即将一个对象的属性值赋给另一个对象.通常我们可以进行如下操作: A a=new A(); a ...