LBTabBarController.m
4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// LBTabBarController.m
// XianYu
//
// Created by li bo on 16/5/28.
// Copyright © 2016年 li bo. All rights reserved.
//
#import "LBTabBarController.h"
#import "LBNavigationController.h"
#import "LBFishViewController.h"
#import "LBMineViewController.h"
#import "LBpostViewController.h"
#import "LBMessageViewController.h"
#import "LBTabBar.h"
#import "UIImage+Image.h"
#import "GLD_UIBuilder.h"
#import "GLD_Mediator+Root.h"
@interface LBTabBarController ()<LBTabBarDelegate>
@end
@implementation LBTabBarController
#pragma mark - 第一次使用当前类的时候对设置UITabBarItem的主题
+ (void)initialize
{
UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
dictNormal[NSForegroundColorAttributeName] = [UIColor grayColor];
dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:11];
NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
dictSelected[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:11];
[tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
[tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpAllChildVc];
//创建自己的tabbar,然后用kvc将自己的tabbar和系统的tabBar替换下
LBTabBar *tabbar = [[LBTabBar alloc] init];
tabbar.myDelegate = self;
//kvc实质是修改了系统的_tabBar
[self setValue:tabbar forKeyPath:@"tabBar"];
}
#pragma mark - ------------------------------------------------------------------
#pragma mark - 初始化tabBar上除了中间按钮之外所有的按钮
- (void)setUpAllChildVc
{
UIViewController *HomeVC = [GLD_Router homeViewControllorWithTitle:@"首页"];
[self setUpOneChildVcWithVc:HomeVC Image:@"home_normal" selectedImage:@"home_highlight" title:@""];
UIViewController *FishVC = [GLD_Router otherViewControllerWithTitle:@"其他" onClick:^{
NSLog(@"dianjila");
}];
[self setUpOneChildVcWithVc:FishVC Image:@"fish_normal" selectedImage:@"fish_highlight" title:@"鱼塘"];
LBMessageViewController *MessageVC = [[LBMessageViewController alloc] init];
[self setUpOneChildVcWithVc:MessageVC Image:@"message_normal" selectedImage:@"message_highlight" title:@"消息"];
LBMineViewController *MineVC = [[LBMineViewController alloc] init];
[self setUpOneChildVcWithVc:MineVC Image:@"account_normal" selectedImage:@"account_highlight" title:@"我的"];
}
#pragma mark - 初始化设置tabBar上面单个按钮的方法
/**
* @author li bo, 16/05/10
*
* 设置单个tabBarButton
*
* @param Vc 每一个按钮对应的控制器
* @param image 每一个按钮对应的普通状态下图片
* @param selectedImage 每一个按钮对应的选中状态下的图片
* @param title 每一个按钮对应的标题
*/
- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
{
LBNavigationController *nav = [GLD_UIBuilder navigationControllerWithRootVC:Vc];
UIImage *myImage = [UIImage imageNamed:image];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//tabBarItem,是系统提供模型,专门负责tabbar上按钮的文字以及图片展示
// Vc.tabBarItem.image = myImage;
UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
Vc.tabBarItem.selectedImage = mySelectedImage;
// Vc.tabBarItem.title = title;
// Vc.navigationItem.title = title;
[self addChildViewController:nav];
}
#pragma mark - ------------------------------------------------------------------
#pragma mark - LBTabBarDelegate
//点击中间按钮的代理方法
- (void)tabBarPlusBtnClick:(LBTabBar *)tabBar
{
LBpostViewController *plusVC = [[LBpostViewController alloc] init];
LBNavigationController *navVc = [[LBNavigationController alloc] initWithRootViewController:plusVC];
[self presentViewController:navVc animated:YES completion:nil];
}
@end