forked from ChenYilong/CYLTabBarController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCYLTabBarControllerConfig.m
212 lines (183 loc) · 9.89 KB
/
CYLTabBarControllerConfig.m
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// CYLTabBarControllerConfig.m
// CYLTabBarController
//
// Created by 微博@iOS程序犭袁 ( http://weibo.com/luohanchenyilong/ ) on 10/20/15.
// Copyright © 2015 https://github.com/ChenYilong . All rights reserved.
//
#import "CYLTabBarControllerConfig.h"
@import Foundation;
@import UIKit;
@interface CYLBaseNavigationController : UINavigationController
@end
@implementation CYLBaseNavigationController
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (self.viewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
@end
//View Controllers
#import "CYLHomeViewController.h"
#import "CYLMessageViewController.h"
#import "CYLMineViewController.h"
#import "CYLSameCityViewController.h"
@interface CYLTabBarControllerConfig ()
@property (nonatomic, readwrite, strong) CYLTabBarController *tabBarController;
@end
@implementation CYLTabBarControllerConfig
/**
* lazy load tabBarController
*
* @return CYLTabBarController
*/
- (CYLTabBarController *)tabBarController {
if (_tabBarController == nil) {
CYLTabBarController *tabBarController = [CYLTabBarController tabBarControllerWithViewControllers:self.viewControllers
tabBarItemsAttributes:self.tabBarItemsAttributesForController];
[self customizeTabBarAppearance:tabBarController];
_tabBarController = tabBarController;
}
return _tabBarController;
}
- (NSArray *)viewControllers {
CYLHomeViewController *firstViewController = [[CYLHomeViewController alloc] init];
UIViewController *firstNavigationController = [[CYLBaseNavigationController alloc]
initWithRootViewController:firstViewController];
CYLSameCityViewController *secondViewController = [[CYLSameCityViewController alloc] init];
UIViewController *secondNavigationController = [[CYLBaseNavigationController alloc]
initWithRootViewController:secondViewController];
CYLMessageViewController *thirdViewController = [[CYLMessageViewController alloc] init];
UIViewController *thirdNavigationController = [[CYLBaseNavigationController alloc]
initWithRootViewController:thirdViewController];
CYLMineViewController *fourthViewController = [[CYLMineViewController alloc] init];
UIViewController *fourthNavigationController = [[CYLBaseNavigationController alloc]
initWithRootViewController:fourthViewController];
/**
* 以下两行代码目的在于手动设置让TabBarItem只显示图标,不显示文字,并让图标垂直居中。
* 等效于在 `-tabBarItemsAttributesForController` 方法中不传 `CYLTabBarItemTitle` 字段。
* 更推荐后一种做法。
*/
//tabBarController.imageInsets = UIEdgeInsetsMake(4.5, 0, -4.5, 0);
//tabBarController.titlePositionAdjustment = UIOffsetMake(0, MAXFLOAT);
NSArray *viewControllers = @[
firstNavigationController,
secondNavigationController,
thirdNavigationController,
fourthNavigationController
];
return viewControllers;
}
- (NSArray *)tabBarItemsAttributesForController {
NSDictionary *firstTabBarItemsAttributes = @{
// CYLTabBarItemTitle : @"首页",
CYLTabBarItemImage : @"home_normal",
CYLTabBarItemSelectedImage : @"home_highlight",
};
NSDictionary *secondTabBarItemsAttributes = @{
// CYLTabBarItemTitle : @"同城",
CYLTabBarItemImage : @"mycity_normal",
CYLTabBarItemSelectedImage : @"mycity_highlight",
};
NSDictionary *thirdTabBarItemsAttributes = @{
// CYLTabBarItemTitle : @"消息",
CYLTabBarItemImage : @"message_normal",
CYLTabBarItemSelectedImage : @"message_highlight",
};
NSDictionary *fourthTabBarItemsAttributes = @{
// CYLTabBarItemTitle : @"我的",
CYLTabBarItemImage : @"account_normal",
CYLTabBarItemSelectedImage : @"account_highlight"
};
NSArray *tabBarItemsAttributes = @[
firstTabBarItemsAttributes,
secondTabBarItemsAttributes,
thirdTabBarItemsAttributes,
fourthTabBarItemsAttributes
];
return tabBarItemsAttributes;
}
/**
* 更多TabBar自定义设置:比如:tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性等等
*/
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
#warning CUSTOMIZE YOUR TABBAR APPEARANCE
// Customize UITabBar height
// 自定义 TabBar 高度
tabBarController.tabBarHeight = 40.f;
// set the text color for unselected state
// 普通状态下的文字属性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
// set the text color for selected state
// 选中状态下的文字属性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
// set the text Attributes
// 设置文字属性
UITabBarItem *tabBar = [UITabBarItem appearance];
[tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
[tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
// Set the dark color to selected tab (the dimmed background)
// TabBarItem选中后的背景颜色
// [self customizeTabBarSelectionIndicatorImage];
// update TabBar when TabBarItem width did update
// If your app need support UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight,
// remove the comment '//'
// 如果你的App需要支持横竖屏,请使用该方法移除注释 '//'
// [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];
// set the bar shadow image
// This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
[[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tapbar_top_line"]];
// set the bar background image
// 设置背景图片
// UITabBar *tabBarAppearance = [UITabBar appearance];
// [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background"]];
// remove the bar system shadow image
// 去除 TabBar 自带的顶部阴影
// [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
}
- (void)updateTabBarCustomizationWhenTabBarItemWidthDidUpdate {
void (^deviceOrientationDidChangeBlock)(NSNotification *) = ^(NSNotification *notification) {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIDeviceOrientationLandscapeLeft) || (orientation == UIDeviceOrientationLandscapeRight)) {
NSLog(@"Landscape Left or Right !");
} else if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"Landscape portrait!");
}
[self customizeTabBarSelectionIndicatorImage];
};
[[NSNotificationCenter defaultCenter] addObserverForName:CYLTabBarItemWidthDidChangeNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:deviceOrientationDidChangeBlock];
}
- (void)customizeTabBarSelectionIndicatorImage {
///Get initialized TabBar Height if exists, otherwise get Default TabBar Height.
UITabBarController *tabBarController = [self cyl_tabBarController] ?: [[UITabBarController alloc] init];
CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;
CGSize selectionIndicatorImageSize = CGSizeMake(CYLTabBarItemWidth, tabBarHeight);
//Get initialized TabBar if exists.
UITabBar *tabBar = [self cyl_tabBarController].tabBar ?: [UITabBar appearance];
[tabBar setSelectionIndicatorImage:
[[self class] imageWithColor:[UIColor redColor]
size:selectionIndicatorImageSize]];
}
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
if (!color || size.width <= 0 || size.height <= 0) return nil;
CGRect rect = CGRectMake(0.0f, 0.0f, size.width + 1, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end