-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCustomUITabBarController.m
354 lines (312 loc) · 9.35 KB
/
CustomUITabBarController.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// CustomUITabBarController.m
// CustomUITabBar
//
// Created by akron on 12/18/13.
// Copyright (c) 2013 yechunxiao. All rights reserved.
//
#import "CustomUITabBarController.h"
#import "CustomTabBar.h"
#define kTabBarHeight 49.0f
static CustomUITabBarController *customUITabBarController;
@implementation UIViewController (CustomUITabBarControllerSupport)
- (CustomUITabBarController *)customUITabBarController
{
return customUITabBarController;
}
@end
@interface CustomUITabBarController ()
- (void)displayViewAtIndex:(NSUInteger)index;
@end
@implementation CustomUITabBarController
@synthesize delegate = _delegate;
@synthesize selectedViewController = _selectedViewController;
@synthesize viewControllers = _viewControllers;
@synthesize selectedIndex = _selectedIndex;
@synthesize tabBarHidden = _tabBarHidden;
@synthesize animateDriect;
#pragma mark -
#pragma mark lifecycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (id)initWithViewControllers:(NSArray *)vcs imageArray:(NSArray *)arr
{
self = [super init];
if (self != nil)
{
_viewControllers = [NSMutableArray arrayWithArray:vcs];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
_containerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
else{
_containerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
_transitionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320.0f, _containerView.frame.size.height - kTabBarHeight)];
_transitionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
_tabBar = [[CustomTabBar alloc] initWithFrame:CGRectMake(0, _containerView.frame.size.height - kTabBarHeight, 320.0f, kTabBarHeight) buttonImages:arr];
_tabBar.delegate = self;
customUITabBarController = self;
animateDriect = 0;
}
return self;
}
- (void)loadView
{
[super loadView];
[_containerView addSubview:_transitionView];
[_containerView addSubview:_tabBar];
self.view = _containerView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedIndex = 0;
}
- (void)viewDidUnload
{
[super viewDidUnload];
_tabBar = nil;
_viewControllers = nil;
}
#pragma mark - instant methods
- (CustomTabBar *)tabBar
{
return _tabBar;
}
- (BOOL)tabBarTransparent
{
return _tabBarTransparent;
}
- (void)setTabBarTransparent:(BOOL)yesOrNo
{
if (yesOrNo == YES)
{
_transitionView.frame = _containerView.bounds;
}
else
{
_transitionView.frame = CGRectMake(0, 0, 320.0f, _containerView.frame.size.height - kTabBarHeight);
}
}
//- (void)hidesTabBar:(BOOL)yesOrNO animated:(BOOL)animated
//{
// if (yesOrNO == YES)
// {
// if (self.tabBar.frame.origin.y == self.view.frame.size.height)
// {
// return;
// }
// }
// else
// {
// if (self.tabBar.frame.origin.y == self.view.frame.size.height - kTabBarHeight)
// {
// return;
// }
// }
//
// if (animated == YES)
// {
// [UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.3f];
// if (yesOrNO == YES)
// {
// self.tabBar.frame = CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y + kTabBarHeight, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
// }
// else
// {
// self.tabBar.frame = CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y - kTabBarHeight, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
// }
// [UIView commitAnimations];
// }
// else
// {
// if (yesOrNO == YES)
// {
// self.tabBar.frame = CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y + kTabBarHeight, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
// }
// else
// {
// self.tabBar.frame = CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y - kTabBarHeight, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
// }
// }
//}
- (void)hidesTabBar:(BOOL)yesOrNO animated:(BOOL)animated
{
[self hidesTabBar:yesOrNO animated:animated driect:animateDriect];
}
- (void)hidesTabBar:(BOOL)yesOrNO animated:(BOOL)animated driect:(NSInteger)driect
{
// driect: 0 -- 上下 1 -- 左右
NSInteger kTabBarWidth = [[UIScreen mainScreen] applicationFrame].size.width;
if (yesOrNO == YES)
{
if (driect == 0)
{
if (self.tabBar.frame.origin.y == self.view.frame.size.height)
{
return;
}
}
else
{
if (self.tabBar.frame.origin.x == 0 - kTabBarWidth)
{
return;
}
}
}
else
{
if (driect == 0)
{
if (self.tabBar.frame.origin.y == self.view.frame.size.height - kTabBarHeight)
{
return;
}
}
else
{
if (self.tabBar.frame.origin.x == 0)
{
return;
}
}
}
if (animated == YES)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
if (yesOrNO == YES)
{
if (driect == 0)
{
self.tabBar.frame = CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y + kTabBarHeight, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}
else
{
self.tabBar.frame = CGRectMake(0 - kTabBarWidth, self.tabBar.frame.origin.y, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}
}
else
{
if (driect == 0)
{
self.tabBar.frame = CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y - kTabBarHeight, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}
else
{
self.tabBar.frame = CGRectMake(0, self.tabBar.frame.origin.y, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}
}
[UIView commitAnimations];
}
else
{
if (yesOrNO == YES)
{
if (driect == 0)
{
self.tabBar.frame = CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y + kTabBarHeight, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}
else
{
self.tabBar.frame = CGRectMake(0 - kTabBarWidth, self.tabBar.frame.origin.y, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}
}
else
{
if (driect == 0)
{
self.tabBar.frame = CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y - kTabBarHeight, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}
else
{
self.tabBar.frame = CGRectMake(0, self.tabBar.frame.origin.y, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}
}
}
}
- (NSUInteger)selectedIndex
{
return _selectedIndex;
}
- (UIViewController *)selectedViewController
{
return [_viewControllers objectAtIndex:_selectedIndex];
}
-(void)setSelectedIndex:(NSUInteger)index
{
[self displayViewAtIndex:index];
[_tabBar selectTabAtIndex:index];
}
- (void)removeViewControllerAtIndex:(NSUInteger)index
{
if (index >= [_viewControllers count])
{
return;
}
// Remove view from superview.
[[(UIViewController *)[_viewControllers objectAtIndex:index] view] removeFromSuperview];
// Remove viewcontroller in array.
[_viewControllers removeObjectAtIndex:index];
// Remove tab from tabbar.
[_tabBar removeTabAtIndex:index];
}
- (void)insertViewController:(UIViewController *)vc withImageDic:(NSDictionary *)dict atIndex:(NSUInteger)index
{
[_viewControllers insertObject:vc atIndex:index];
[_tabBar insertTabWithImageDic:dict atIndex:index];
}
#pragma mark - Private methods
- (void)displayViewAtIndex:(NSUInteger)index
{
// Before change index, ask the delegate should change the index.
if ([_delegate respondsToSelector:@selector(tabBarController:shouldSelectViewController:)])
{
if (![_delegate tabBarController:self shouldSelectViewController:[self.viewControllers objectAtIndex:index]])
{
return;
}
}
// If target index if equal to current index, do nothing.
if (_selectedIndex == index && [[_transitionView subviews] count] != 0)
{
return;
}
NSLog(@"Display View.");
_selectedIndex = index;
UIViewController *selectedVC = [self.viewControllers objectAtIndex:index];
selectedVC.view.frame = _transitionView.frame;
if ([selectedVC.view isDescendantOfView:_transitionView])
{
[_transitionView bringSubviewToFront:selectedVC.view];
}
else
{
[_transitionView addSubview:selectedVC.view];
}
// Notify the delegate, the viewcontroller has been changed.
if ([_delegate respondsToSelector:@selector(tabBarController:didSelectViewController::)])
{
[_delegate tabBarController:self didSelectViewController:selectedVC];
}
}
#pragma mark -
#pragma mark tabBar delegates
- (void)tabBar:(CustomTabBar *)tabBar didSelectIndex:(NSInteger)index
{
if (self.selectedIndex == index) {
UINavigationController *nav = [self.viewControllers objectAtIndex:index];
[nav popToRootViewControllerAnimated:YES];
}else {
[self displayViewAtIndex:index];
}
}
@end