-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAKTabBarController.m
More file actions
executable file
·383 lines (295 loc) · 11.9 KB
/
Copy pathAKTabBarController.m
File metadata and controls
executable file
·383 lines (295 loc) · 11.9 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// AKTabBarController.m
//
// Copyright (c) 2012 Ali Karagoz (http://alikaragoz.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "AKTabBarController.h"
#import "UIViewController+AKTabBarController.h"
// Default height of the tab bar
static const int kDefaultTabBarHeight = 50;
// Default Push animation duration
static const float kPushAnimationDuration = 0.35;
@interface AKTabBarController ()
{
NSArray *prevViewControllers;
BOOL visible;
}
typedef enum {
AKShowHideFromLeft,
AKShowHideFromRight
} AKShowHideFrom;
// Current active view controller
@property (nonatomic, strong) UIViewController *selectedViewController;
- (void)loadTabs;
- (void)showTabBar:(AKShowHideFrom)showHideFrom animated:(BOOL)animated;
- (void)hideTabBar:(AKShowHideFrom)showHideFrom animated:(BOOL)animated;
@end
@implementation AKTabBarController
{
// Bottom tab bar view
AKTabBar *tabBar;
// Content view
AKTabBarView *tabBarView;
// Tab Bar height
NSUInteger tabBarHeight;
}
#pragma mark - Initialization
- (id)init
{
self = [super init];
if (!self) return nil;
// Setting the default tab bar height
tabBarHeight = kDefaultTabBarHeight;
return self;
}
- (id)initWithTabBarHeight:(NSUInteger)height
{
self = [super init];
if (!self) return nil;
tabBarHeight = height;
return self;
}
- (void)loadView
{
[super loadView];
// Creating and adding the tab bar view
tabBarView = [[AKTabBarView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = tabBarView;
// Creating and adding the tab bar
CGRect tabBarRect = CGRectMake(0.0, CGRectGetHeight(self.view.bounds) - tabBarHeight, CGRectGetWidth(self.view.frame), tabBarHeight);
tabBar = [[AKTabBar alloc] initWithFrame:tabBarRect];
tabBar.delegate = self;
tabBarView.tabBar = tabBar;
tabBarView.contentView = _selectedViewController.view;
[[self navigationItem] setTitle:[_selectedViewController title]];
[self loadTabs];
}
- (void)loadTabs
{
NSMutableArray *tabs = [[NSMutableArray alloc] init];
for (UIViewController *vc in self.viewControllers)
{
[[tabBarView tabBar] setBackgroundImageName:[self backgroundImageName]];
[[tabBarView tabBar] setTabColors:[self tabCGColors]];
[[tabBarView tabBar] setEdgeColor:[self tabEdgeColor]];
AKTab *tab = [[AKTab alloc] init];
[tab setTabImageWithName:[vc tabImageName]];
[tab setBackgroundImageName:[self backgroundImageName]];
[tab setSelectedBackgroundImageName:[self selectedBackgroundImageName]];
[tab setTabIconColors:[self iconCGColors]];
[tab setTabIconColorsSelected:[self selectedIconCGColors]];
[tab setTabSelectedColors:[self selectedTabCGColors]];
[tab setEdgeColor:[self tabEdgeColor]];
[tab setGlossyIsHidden:[self iconGlossyIsHidden]];
[tab setStrokeColor:[self tabStrokeColor]];
[tab setTextColor:[self textColor]];
[tab setSelectedTextColor:[self selectedTextColor]];
[tab setTabTitle:[vc tabTitle]];
[tab setTabBarHeight:tabBarHeight];
if (_minimumHeightToDisplayTitle)
[tab setMinimumHeightToDisplayTitle:_minimumHeightToDisplayTitle];
if (_tabTitleIsHidden)
[tab setTitleIsHidden:YES];
if ([[vc class] isSubclassOfClass:[UINavigationController class]])
((UINavigationController *)vc).delegate = self;
[tabs addObject:tab];
}
[tabBar setTabs:tabs];
// Setting the first view controller as the active one
[tabBar setSelectedTab:[tabBar.tabs objectAtIndex:0]];
}
- (NSArray *) selectedIconCGColors
{
return _selectedIconColors ? @[(id)[[_selectedIconColors objectAtIndex:0] CGColor], (id)[[_selectedIconColors objectAtIndex:1] CGColor]] : nil;
}
- (NSArray *) iconCGColors
{
return _iconColors ? @[(id)[[_iconColors objectAtIndex:0] CGColor], (id)[[_iconColors objectAtIndex:1] CGColor]] : nil;
}
- (NSArray *) tabCGColors
{
return _tabColors ? @[(id)[[_tabColors objectAtIndex:0] CGColor], (id)[[_tabColors objectAtIndex:1] CGColor]] : nil;
}
- (NSArray *) selectedTabCGColors
{
return _selectedTabColors ? @[(id)[[_selectedTabColors objectAtIndex:0] CGColor], (id)[[_selectedTabColors objectAtIndex:1] CGColor]] : nil;
}
#pragma - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (!prevViewControllers)
prevViewControllers = [navigationController viewControllers];
// We detect is the view as been push or popped
BOOL pushed;
if ([prevViewControllers count] <= [[navigationController viewControllers] count])
pushed = YES;
else
pushed = NO;
// Logic to know when to show or hide the tab bar
BOOL isPreviousHidden, isNextHidden;
isPreviousHidden = [[prevViewControllers lastObject] hidesBottomBarWhenPushed];
isNextHidden = [viewController hidesBottomBarWhenPushed];
prevViewControllers = [navigationController viewControllers];
if (!isPreviousHidden && !isNextHidden)
return;
else if (!isPreviousHidden && isNextHidden)
[self hideTabBar:(pushed ? AKShowHideFromRight : AKShowHideFromLeft) animated:animated];
else if (isPreviousHidden && !isNextHidden)
[self showTabBar:(pushed ? AKShowHideFromRight : AKShowHideFromLeft) animated:animated];
else if (isPreviousHidden && isNextHidden)
return;
}
- (void)showTabBar:(AKShowHideFrom)showHideFrom animated:(BOOL)animated
{
CGFloat directionVector;
switch (showHideFrom) {
case AKShowHideFromLeft:
directionVector = -1.0;
break;
case AKShowHideFromRight:
directionVector = 1.0;
break;
default:
break;
}
tabBar.hidden = NO;
tabBar.transform = CGAffineTransformMakeTranslation(CGRectGetWidth(self.view.bounds) * directionVector, 0);
// when the tabbarview is resized we can see the view behind
[UIView animateWithDuration:((animated) ? kPushAnimationDuration : 0) animations:^{
tabBar.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
tabBarView.isTabBarHidding = NO;
[tabBarView setNeedsLayout];
}];
}
- (void)hideTabBar:(AKShowHideFrom)showHideFrom animated:(BOOL)animated
{
CGFloat directionVector;
switch (showHideFrom) {
case AKShowHideFromLeft:
directionVector = 1.0;
break;
case AKShowHideFromRight:
directionVector = -1.0;
break;
default:
break;
}
tabBarView.isTabBarHidding = YES;
CGRect tmpTabBarView = tabBarView.contentView.frame;
tmpTabBarView.size.height = tabBarView.bounds.size.height;
tabBarView.contentView.frame = tmpTabBarView;
[UIView animateWithDuration:((animated) ? kPushAnimationDuration : 0) animations:^{
tabBar.transform = CGAffineTransformMakeTranslation(CGRectGetWidth(self.view.bounds) * directionVector, 0);
} completion:^(BOOL finished) {
tabBar.hidden = YES;
tabBar.transform = CGAffineTransformIdentity;
}];
}
#pragma mark - Setters
- (void)setViewControllers:(NSMutableArray *)viewControllers
{
_viewControllers = viewControllers;
// When setting the view controllers, the first vc is the selected one;
[self setSelectedViewController:[viewControllers objectAtIndex:0]];
}
- (void)setSelectedViewController:(UIViewController *)selectedViewController
{
UIViewController *previousSelectedViewController = selectedViewController;
if (_selectedViewController != selectedViewController)
{
_selectedViewController = selectedViewController;
selectedViewController = selectedViewController;
if (!self.childViewControllers && visible)
{
[previousSelectedViewController viewWillDisappear:NO];
[selectedViewController viewWillAppear:NO];
}
[tabBarView setContentView:selectedViewController.view];
if (!self.childViewControllers && visible)
{
[previousSelectedViewController viewDidDisappear:NO];
[selectedViewController viewDidAppear:NO];
}
[tabBar setSelectedTab:[tabBar.tabs objectAtIndex:[self.viewControllers indexOfObject:selectedViewController]]];
}
}
#pragma mark - Required Protocol Method
- (void)tabBar:(AKTabBar *)AKTabBarDelegate didSelectTabAtIndex:(NSInteger)index
{
UIViewController *vc = [self.viewControllers objectAtIndex:index];
if (self.selectedViewController == vc)
{
if ([vc isKindOfClass:[UINavigationController class]])
[(UINavigationController *)self.selectedViewController popToRootViewControllerAnimated:YES];
}
else
{
[[self navigationItem] setTitle:[vc title]];
self.selectedViewController = vc;
}
}
#pragma mark - Rotation Events
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
// Redraw with will rotating and keeping the aspect ratio
for (AKTab *tab in [tabBar tabs])
[tab setNeedsDisplay];
[self.selectedViewController willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.selectedViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
#pragma mark - ViewController Life cycle
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.childViewControllers)
[self.selectedViewController viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (!self.childViewControllers)
[self.selectedViewController viewDidAppear:animated];
visible = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (!self.childViewControllers)
[self.selectedViewController viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if (![self respondsToSelector:@selector(addChildViewController:)])
[self.selectedViewController viewDidDisappear:animated];
visible = NO;
}
@end