-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOBTabBarController.m
405 lines (306 loc) · 14.6 KB
/
OBTabBarController.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//
// OBTabBarController.m
//
// Created by Oriol Blanc on 11/22/11.
// Copyright (c) 2011 Oriol Blanc. All rights reserved.
//
#import "OBTabBarController.h"
#define kNoViewControllerSelected -1
@interface OBTabBarController ()
@property (nonatomic) NSUInteger selectedIndexInternal; // To store the previous selected index when the view is unloaded
@property (nonatomic, strong) UIView *tabBar;
@property (nonatomic, strong) UIView *viewForVisibleViewController;
@property (nonatomic) BOOL tabBarHidden;
@property (nonatomic) BOOL tabBarHiddenBeforePresentingModalViewController;
@property (nonatomic, strong) NSMutableArray *tabBarButtons;
@property (nonatomic, strong) NSMutableArray *tabBarLabels;
@property (nonatomic, copy) NSArray *tabBarImages;
@property (nonatomic, copy) NSArray *selectedTabBarImages;
@property (nonatomic, strong) UIImage *backgroundImage;
- (UIColor *)textColor;
- (UIColor *)textHighlightedColor;
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated;
@end
@implementation OBTabBarController
- (id)initWithViewControllers:(NSArray *)viewControllers delegate:(id <OBTabBarControllerDelegate>)delegate
{
if ((self = [super init]))
{
if (delegate == nil)
return self;
self.delegate = delegate;
self.viewControllers = viewControllers;
self.tabBarButtons = [NSMutableArray arrayWithCapacity:self.viewControllers.count];
self.tabBarLabels = [NSMutableArray arrayWithCapacity:self.viewControllers.count];
_selectedIndex = kNoViewControllerSelected;
_selectedIndexInternal = kNoViewControllerSelected;
self.view.frame = CGRectMake(0, 20, 320, self.view.bounds.size.height - 20);
}
return self;
}
- (void)loadView
{
if (self.delegate != nil && [self.delegate conformsToProtocol:@protocol(OBTabBarControllerDelegate)])
{
if ([self.delegate respondsToSelector:@selector(tabBarBackground)])
{
self.backgroundImage = [self.delegate tabBarBackground];
}
NSMutableArray *images = [NSMutableArray array];
NSMutableArray *highlighedImages = [NSMutableArray array];
for (NSUInteger index = 0; index < self.viewControllers.count; index++)
{
UIImage *tabBarIcon = [self.delegate imageTabAtIndex:index];
if(tabBarIcon != nil)
{
[images addObject:tabBarIcon];
}
if ([self.delegate respondsToSelector:@selector(highlightedImageTabAtIndex:)])
{
UIImage *tabBarHighlightedIcon = [self.delegate highlightedImageTabAtIndex:index];
if(tabBarHighlightedIcon != nil)
{
[highlighedImages addObject:tabBarHighlightedIcon];
}
}
}
self.tabBarImages = images;
self.selectedTabBarImages = highlighedImages;
}
CGRect tabBarControllerFrame = [self frameForTabBarControllerView];
UIView *mainView = [[UIView alloc] initWithFrame:tabBarControllerFrame];
mainView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
mainView.backgroundColor = [UIColor clearColor];
UIView *tabBarView = self.tabBar;
[mainView addSubview:self.viewForVisibleViewController];
[mainView addSubview:tabBarView];
self.view = mainView;
if (_selectedIndexInternal != kNoViewControllerSelected)
{
[self setSelectedIndex:_selectedIndexInternal];
_selectedIndexInternal = kNoViewControllerSelected;
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (_selectedIndex == kNoViewControllerSelected)
{
self.selectedIndex = 0;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
__weak typeof(self) blockSafeSelf = self;
[[NSNotificationCenter defaultCenter] addObserverForName:@"UINavigationControllerWillShowViewControllerNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
NSDictionary *transitionInfo = note.userInfo;
UIViewController *willShowViewController = [transitionInfo valueForKey:@"UINavigationControllerNextVisibleViewController"];
BOOL isTheFirstViewControllerInTheNavigationStack = willShowViewController == [willShowViewController.navigationController.viewControllers objectAtIndex:0];
BOOL controllerIsBeingPresentedModally = NO;
BOOL hide = (willShowViewController.hidesBottomBarWhenPushed && !isTheFirstViewControllerInTheNavigationStack) || controllerIsBeingPresentedModally;
BOOL animated = [[transitionInfo valueForKey:@"UINavigationControllerTransitionIsAnimated"] boolValue];
[blockSafeSelf setTabBarHidden:hide animated:animated];
}];
}
- (CGRect)frameForTabBarControllerView
{
CGRect tabBarControllerFrame = [[UIScreen mainScreen] bounds];
return tabBarControllerFrame;
}
- (UIView *)tabBar
{
if (!_tabBar)
{
CGRect tabBarControllerFrame = [self frameForTabBarControllerView];
_tabBar = [[UIView alloc] initWithFrame:CGRectMake(0, tabBarControllerFrame.size.height - [[self class] tabBarHeight], tabBarControllerFrame.size.width, [[self class] tabBarHeight])];
_tabBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_tabBar.backgroundColor = [UIColor whiteColor];
UIImageView *tabBarBackground = [[UIImageView alloc] initWithImage:self.backgroundImage];
tabBarBackground.frame = _tabBar.bounds;
[_tabBar addSubview:tabBarBackground];
CGFloat buttonLeftMargin = 0;
CGFloat buttonWidth = _tabBar.frame.size.width / (float)self.viewControllers.count;
CGFloat buttonHeight = _tabBar.frame.size.height;
[self.tabBarLabels removeAllObjects];
[self.tabBarButtons removeAllObjects];
for (int i = 0; i < self.viewControllers.count; i++)
{
UIImage *tabImage = nil;
if(self.tabBarImages.count > i)
{
tabImage = [self.tabBarImages objectAtIndex:i];
}
CGRect buttonFrame = CGRectMake(buttonLeftMargin, 0, buttonWidth, buttonHeight);
UIButton *tabBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
tabBarButton.tag = i;
[tabBarButton addTarget:self action:@selector(tabBarButtonPressed:) forControlEvents:UIControlEventTouchDown];
tabBarButton.frame = buttonFrame;
[tabBarButton setImage:tabImage forState:UIControlStateNormal];
tabBarButton.adjustsImageWhenHighlighted = NO;
[_tabBar addSubview:tabBarButton];
[self.tabBarButtons addObject:tabBarButton];
if ([self.delegate respondsToSelector:@selector(shouldShowLabelForIndex:)])
{
if ([self.delegate shouldShowLabelForIndex:i])
{
CGFloat fontPointSize;
UILabel *titleLabel;
static CGFloat padding = 2.0f;
fontPointSize = [self textFont].pointSize;
titleLabel = [[UILabel alloc] initWithFrame:(CGRect){
.origin.x = buttonLeftMargin,
.origin.y = CGRectGetHeight(_tabBar.frame) - fontPointSize - padding,
.size.width = buttonWidth,
.size.height = fontPointSize
}];
titleLabel.textColor = [self textColor];
titleLabel.highlightedTextColor = [self textHighlightedColor];
titleLabel.font = [self textFont];
titleLabel.text = ((UIViewController *)self.viewControllers[i]).title;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
[_tabBar addSubview:titleLabel];
[self.tabBarLabels addObject:titleLabel];
}
}
buttonLeftMargin += buttonWidth;
}
}
return _tabBar;
}
- (void)tabBarButtonPressed:(UIButton *)button
{
self.selectedIndex = button.tag;
}
- (UIView *)viewForVisibleViewController
{
if (!_viewForVisibleViewController)
{
CGRect tabBarControllerFrame = [self frameForTabBarControllerView];
_viewForVisibleViewController = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tabBarControllerFrame.size.width, tabBarControllerFrame.size.height - self.tabBar.frame.size.height)];
_viewForVisibleViewController.backgroundColor = [UIColor clearColor];
_viewForVisibleViewController.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_viewForVisibleViewController.clipsToBounds = YES;
}
return _viewForVisibleViewController;
}
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated
{
CGFloat viewForVisibleViewControllerHeight = hidden ? self.view.frame.size.height : self.view.frame.size.height - self.tabBar.frame.size.height;
_viewForVisibleViewController.frame = CGRectMake(0, 0, self.view.frame.size.width, viewForVisibleViewControllerHeight);
static const CGFloat kPushAnimationDuration = 0.35;
[UIView animateWithDuration:(animated ? kPushAnimationDuration : 0) delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
self.tabBar.alpha = hidden ? 0.0 : 1.0;
} completion:^(BOOL finished) {
if (finished)
{
self.tabBarHidden = hidden;
}
}];
}
- (UIColor *)textColor
{
return [[UITabBarItem appearance] titleTextAttributesForState:UIControlStateNormal][UITextAttributeTextColor] ? : [UIColor grayColor];
}
- (UIColor *)textHighlightedColor
{
return [[UITabBarItem appearance] titleTextAttributesForState:UIControlStateSelected][UITextAttributeTextColor] ? : [UIColor whiteColor];
}
- (UIFont *)textFont
{
return [[UITabBarItem appearance] titleTextAttributesForState:UIControlStateNormal][UITextAttributeFont] ? :
[UIFont boldSystemFontOfSize:10.0f];
}
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
[super presentViewController:modalViewController animated:animated completion:^{
self.tabBarHiddenBeforePresentingModalViewController = self.tabBarHidden;
}];
}
- (void)dismissModalViewControllerAnimated:(BOOL)animated
{
[super dismissViewControllerAnimated:animated completion:^{
[self setTabBarHidden:self.tabBarHiddenBeforePresentingModalViewController animated:NO];
}];
}
#pragma mark - View Controllers Logic
- (void)setSelectedIndex:(NSInteger)index
{
NSAssert(index >= 0 && index < self.viewControllers.count, @"Trying to select a view controller off bounds!");
if (self.delegate && [self.delegate respondsToSelector:@selector(shouldSelectTabAtIndex:)] && ![self.delegate shouldSelectTabAtIndex:index])
{
if ([self.delegate respondsToSelector:@selector(didSelectedTabAtIndex:)])
{
[self.delegate didSelectedTabAtIndex:index];
return;
}
}
[self.tabBarButtons setValue:[NSNumber numberWithBool:NO] forKey:@"enabled"]; // Disable all buttons to avoid quick switching between tabs
[self.tabBarButtons setValue:[NSNumber numberWithBool:NO] forKey:@"selected"]; // Deselect all
[[self.tabBarButtons objectAtIndex:index] setSelected:YES];
[self.selectedTabBarImages enumerateObjectsUsingBlock:^(UIImage *selectedTabImage, NSUInteger idx, BOOL *stop) {
UIButton *tabBarButton = self.tabBarButtons[idx];
[tabBarButton setImage:idx != index ? self.tabBarImages[idx] : selectedTabImage
forState:UIControlStateNormal];
}];
[self.tabBarLabels enumerateObjectsUsingBlock:^(UILabel *titleLabel, NSUInteger idx, BOOL *stop) {
titleLabel.highlighted = idx == index;
}];
if (index != _selectedIndex || _selectedIndexInternal != kNoViewControllerSelected)
{
// Remove last view controller from screen:
if (_selectedIndex != kNoViewControllerSelected)
{
[_viewForVisibleViewController.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
UIViewController *viewControllerToAppear = [self.viewControllers objectAtIndex:index];
[_viewForVisibleViewController addSubview:viewControllerToAppear.view];
viewControllerToAppear.view.frame = _viewForVisibleViewController.bounds;
if ([viewControllerToAppear isKindOfClass:[UINavigationController class]])
{
// Adjust (fix) nav bar position:
UINavigationBar *navBar = ((UINavigationController *)viewControllerToAppear).navigationBar;
CGRect navBarFrame = navBar.frame;
navBarFrame.origin.y = 0;
navBar.frame = navBarFrame;
}
_selectedIndex = index;
}
else // Back to root view controller in navigation stack
{
UINavigationController *selectedViewController = [self.viewControllers objectAtIndex:_selectedIndex];
if ([selectedViewController isKindOfClass:[UINavigationController class]])
{
[selectedViewController popToRootViewControllerAnimated:YES];
}
}
[self.tabBarButtons setValue:[NSNumber numberWithBool:YES] forKey:@"enabled"];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
+ (CGFloat)tabBarHeight
{
return 44;
}
#pragma mark - Memory Management
- (void)viewDidUnload
{
self.tabBar = nil;
self.viewForVisibleViewController = nil;
self.backgroundImage = nil;
_selectedIndexInternal = _selectedIndex;
_selectedIndex = kNoViewControllerSelected;
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UINavigationControllerWillShowViewControllerNotification" object:nil];
}
@end
#pragma mark - UIActionSheet Category
@implementation UIActionSheet (OBTabBarActionSheetAdditions)
- (void)showFromTabBar:(OBTabBarController *)tabBarController
{
[self showFromTabBar:((UITabBar *)tabBarController.tabBar)];
}
@end