UITabBarController自定义一
UITabBarController自定义一
- 首先在Appdelegate.m文件中将UITabBarController的子类设置为rootViewController,并设置其viewControllers
1.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2. // Override point for customization after application launch.
3. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
4. self.window.backgroundColor = [UIColor whiteColor];
5. [self.window makeKeyAndVisible];
6.
7. JKBaseTabBarController *baseTabBarController = [self createRootController];
8.
9. self.window.rootViewController = baseTabBarController;
10.
11.
12. return YES;
13.}
14.
15.#pragma mark - 创建跟视图
16.-(JKBaseTabBarController *)createRootController{
17. JKBaseTabBarController *baseTabBarController = [[JKBaseTabBarController alloc] init];
18.
19. // items
20.// NSArray *titles = @[@"总览",@"账户",@"图表"];
21.
22. JKGeneralViewController *general = [[JKGeneralViewController alloc] init];
23. general.tabBarItem.title = @"总览";
24. general.tabBarItem.image = [[UIImage imageNamed:@"overview_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
25. general.tabBarItem.selectedImage = [[UIImage imageNamed:@"overview_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
26.
27. JKAccountViewController *account = [[JKAccountViewController alloc] init];
28. account.tabBarItem.title = @"账户";
29. account.tabBarItem.image = [[UIImage imageNamed:@"account_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
30. account.tabBarItem.selectedImage = [[UIImage imageNamed:@"account_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
31.
32. JKGraphViewController *graph = [[JKGraphViewController alloc] init];
33. graph.tabBarItem.title = @"图表";
34. graph.tabBarItem.image = [[UIImage imageNamed:@"graph_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
35. graph.tabBarItem.selectedImage = [[UIImage imageNamed:@"graph_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
36.
37. baseTabBarController.viewControllers = @[general,account,graph];
38.
39. return baseTabBarController;
40.}
41.
42.
- 然后在UITabBarController子类的viewWillAppear方法中将tabBar的子视图隐藏掉,换上自定义的视图
1.#pragma mark - 自定义
2.-(void)viewWillAppear:(BOOL)animated{
3. [super viewWillAppear:animated];
4. self.tabBar.barTintColor = [UIColor blackColor];
5.
6. // 先将系统自带的隐藏
7. for (UIView *view in self.tabBar.subviews) {
8. view.hidden = YES;
9. }
10.
11. NSArray *controllers = self.viewControllers;
12.
13. // 根据有多少项计算宽度
14. CGFloat h = self.tabBar.bounds.size.height;
15. CGFloat w = h;
16. CGFloat interval = (s_w - controllers.count * h) / (controllers.count + 1);
17.
18. for (int i = 0; i < controllers.count; i++) {
19. UITabBarItem *item = ((UIViewController *)controllers[i]).tabBarItem;
20. UIButton *btn = [[UIButton alloc] initWithFrame:(CGRect){interval + i * (w + interval),0,w,h}];
21.
22. btn.imageEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0);
23.
24. [btn setImage:item.image forState:UIControlStateNormal];
25. [btn setImage:item.selectedImage forState:UIControlStateSelected];
26.
27. btn.titleEdgeInsets = UIEdgeInsetsMake(30, -35, 0, 0);
28. btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
29. [btn setTitle:item.title forState:UIControlStateNormal];
30. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
31. [btn setTitleColor:lightBlueColor forState:UIControlStateSelected];
32.
33.
34. btn.tag = i + 200;
35. [btn addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
36.
37.
38. if (i == 0) {
39. btn.selected = YES;
40. }
41.
42. [self.tabBar addSubview:btn];
43. }
44.}
45.
46.-(void)changeViewController:(UIButton *)sender{
47. if (!sender.selected) {
48. sender.selected = !sender.selected;
49. self.selectedIndex = sender.tag - 200;
50.
51. for (UIView *view in self.tabBar.subviews) {
52. if ([view isKindOfClass:[UIButton class]] && view.tag != sender.tag) {
53. UIButton *btn = (UIButton *)view;
54. btn.selected = NO;
55. }
56. }
57. }
58.
59.}
60.
- 效果如图
*没有上诉自定义的话也可以通过
[general.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateNormal];方法改变title的颜色
UITabBarController自定义一的更多相关文章
- UITabBarController自定义二之xib
UITabBarController自定义二之xib 新建一个xib文件 在UITabBarController的子类方法viewDidLoad方法中加载xib 1.-(void)viewDidLoa ...
- UI学习笔记---第十二天UITabBarController
页签视图控制器-UITabBarController 自定义UITabBar block高级 一.UITabBarController 结构为三层:Tab bar controller v ...
- UIVIewController自定义切换效果-b
之前介绍动画时提过UIView的转场动画,但是开发中我们碰到更多的viewController的切换,ios中常见的viewcontroller切换有四种:模态视图,导航栏控制器,UITabBar ...
- UI入门指引
1. iOS学习路线: C语言:数据类型.流程控制.函数.指针.字符串.结构体.枚举.预处理: OC:面向对象.内存管理.分类.协议.Block.KVC/KVO.Foundation框架: iOS基础 ...
- iOS完整学习步骤
一 C语言 1.1基本数据类型和基本运算 1.2 函数 数组 字符串 指针 1.3 预处理指令 1.4结构体 枚举 1.5 文件操作 内存管理 二 Objective - C 2.1 面向对象 2. ...
- UI-了解ISO
1. iOS学习路线: C语言:数据类型.流程控制.函数.指针.字符串.结构体.枚举.预处理: OC:面向对象.内存管理.分类.协议.Block.KVC/KVO.Foundation框架: iOS基础 ...
- 自定义UITabBarController标签视图控制器
首先创建一个类,继承自UItabBarController 然后在.m文件中: 这里我有两个宏定义: #define WIDTH (myView.frame.size.width / 4) //我在写 ...
- IOS开发之自定义UITabBarController
UITabBarController是开发中经常会用到的一个视图控制器,但是默认的UITabBarController经常不能够完全满足我们的需求,所以我们经常需要自定义一个UITabBarContr ...
- 自定义UITabbarController控制器
自定义UITabbarController控制器 这是定制UITabbarController的基本原理,没有进行功能性封装. 效果: 源码地址: https://github.com/YouXi ...
随机推荐
- Protues中有源蜂鸣器BUZZER不响的解决办法(有图)
这个是BUZZER有源蜂鸣器的protues连线图(FM是我自己的电压探针,可以删除的) 下面是我个人设置的蜂鸣器的参数(为什么很多人的这个蜂鸣器不响,是参数没有设置正确) 蜂鸣器不响的原因是 Ope ...
- Church encoding
In mathematics, Church encoding is a means of representing data and operators in the lambda calculus ...
- build Intent
Intent用于activity之间, fragmetn之间, 或者APP间通信, 主要包含数据和Action两部分: 常见的action是字符串形式的activity指定 Intent intent ...
- 数据结构(二维线段树,差分): NOI2012 魔幻棋盘
貌似想复杂了…… #include <iostream> #include <cstring> #include <cstdio> #define mid ((l+ ...
- 【无源汇上下界最大流】SGU 194 Reactor Cooling
题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=194 题目大意: n个点(n<20000!!!不是200!!!RE了无数次) ...
- 关于sed中的Pattern Space和Hold Space的随笔
首先是一部分概念和示例,这部分转自:http://coolshell.cn/articles/9104.html Pattern Space 第零个是关于-n参数的,大家也许没看懂,没关系,我们来看一 ...
- Redis源码阅读笔记(1)——简单动态字符串sds实现原理
首先,sds即simple dynamic string,redis实现这个的时候使用了一个技巧,并且C99将其收录为标准,即柔性数组成员(flexible array member),参考资料见这里 ...
- word 2010中如何创建多级目录和多级列表
原文地址:http://wenku.baidu.com/link?url=KkSmYTqogxA5VJkLCGb957E5fIGN5S50FUx7IpAWWWKWWRYvaeGl2IvX-dFP25r ...
- Y2错题解析
数据流程图描述信息的来龙去脉和实际流程,反映信息在系统中流动.处理和存储的情况.程序结构图用来描述程序结构,一般由构成系统的要素和表达要素间关系的连线或箭头构成.因果图是一种发现问题"根本原 ...
- Hard 不用+号实现两个数之和 @CareerCup
例子: 759+674 1)不考虑进位: 323 2)只考虑进位:1110 3)两者之和:1433 递归求解c package Hard; /** * Write a function that ...