-
Notifications
You must be signed in to change notification settings - Fork 0
/
RMTopBarController.m
285 lines (212 loc) · 7.73 KB
/
RMTopBarController.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
/*
* This file is part of the RMTopBarController project.
*
* (c) Robert Mogos
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
#import "RMTopBarController.h"
@interface RMTopBarController (PrivateMethods)
- (void)setupTopBar;
- (void)setupContainerView;
- (void)positionTopBar;
@end
@interface UIViewController (RMTopBarControllerItemPrivate)
- (RMTopBarController *) topBarControllerWithPostion:(RMTopBarPosition)postion;
@end
@implementation UIViewController (RMTopBarControllerItem)
- (RMTopBarController *) topBarController{
return [self topBarControllerWithPostion:RMTopBarPositionTop];
}
- (RMTopBarController *) bottomBarController{
return [self topBarControllerWithPostion:RMTopBarPositionBottom];
}
- (RMTopBarController *) topBarControllerWithPostion:(RMTopBarPosition)postion{
UIViewController * ancestor = self.parentViewController;
while (ancestor) {
if ([ancestor isKindOfClass:[RMTopBarController class]] &&
postion == [(RMTopBarController *)ancestor topBarPosition])
return (RMTopBarController *)ancestor;
ancestor = ancestor.parentViewController;
}
return nil;
}
@end
@implementation RMTopBarController
@synthesize selectedIndex = selectedIndex_;
@synthesize delegate = delegate_;
@synthesize topBarXIB;
@synthesize topBar = topBar_;
@synthesize topBarPosition;
- (id)initWithControllers:(NSArray *)controllers{
self = [super initWithNibName:nil bundle:nil];
if (self) {
viewControllers_ = [controllers retain];
selectedIndex_ = -1;
self.topBarXIB = @"RMTopBarView";
topBarPosition = RMTopBarPositionTop;
}
return self;
}
- (void)loadView{
[super loadView];
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask=UIViewAutoresizingFlexibleHeight;
if (nil != self.parentViewController) {
if ([self.parentViewController isKindOfClass:[UINavigationController class]]) {
CGRect frame;
if (![[(UINavigationController *)self.parentViewController navigationBar] isHidden]) {
float height = self.parentViewController.view.frame.size.height;
UINavigationBar *navBar = [(UINavigationController *)self.parentViewController navigationBar];
height -= navBar.frame.size.height;
height -= 20.0;
frame =
(CGRect){self.parentViewController.view.frame.origin,
{self.parentViewController.view.frame.size.width, height}};
}
else frame = self.parentViewController.view.frame;
[self.view setFrame:frame];
}
}
[self.view setBackgroundColor:[UIColor whiteColor]];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad{
[super viewDidLoad];
[self setupTopBar];
[self positionTopBar];
/*
it is important to call - (void)setupContainerView after - (void)setupTopBar
in order to calculate the actual size of the container
*/
[self setupContainerView];
[self selectControllerAtIndex:0 programmatically:YES];
}
#pragma mark compontents init
- (void)setupTopBar{
if (!topBar_) {
topBar_ = [[RMTopBar topBarWithXIB:self.topBarXIB] retain];
[topBar_ setDelegate:self];
[topBar_ setSelectedItemIndex:selectedIndex_];
}
[topBar_ removeFromSuperview];
[self.view addSubview:topBar_];
}
- (void)positionTopBar{
CGPoint position;
if (RMTopBarPositionTop == topBarPosition) {
position = CGPointMake(0.0, 0.0);
}else{
position = CGPointMake(0.0, self.view.frame.size.height - topBar_.frame.size.height);
}
[topBar_ setFrame:(CGRect){position, topBar_.frame.size}];
}
- (void)setupContainerView{
CGRect containerFrame;
if (RMTopBarPositionTop == topBarPosition) {
containerFrame = CGRectMake(0.0,
topBar_.frame.size.height,
self.view.frame.size.width,
self.view.frame.size.height - topBar_.frame.size.height + 20
);
}else{
containerFrame = CGRectMake(0.0,
0.0,
self.view.frame.size.width,
self.view.frame.size.height - topBar_.frame.size.height
);
}
if (!containerView_) {
containerView_ = [[UIView alloc] initWithFrame:containerFrame];
}else{
[containerView_ removeFromSuperview];
}
[containerView_ setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:containerView_];
}
#pragma mark Item Selection
- (void)setSelectedIndex:(NSInteger)selectedIndex{
[self selectControllerAtIndex:selectedIndex programmatically:YES];
}
- (void)selectControllerAtIndex:(NSUInteger)index programmatically:(BOOL) program{
if (index == selectedIndex_ && RMTopBarPositionBottom == topBarPosition && !program) {
if ([currentController_ respondsToSelector:@selector(popToRootViewControllerAnimated:)]) {
[currentController_ performSelector:@selector(popToRootViewControllerAnimated:)
withObject:[NSNumber numberWithBool:YES]];
}else
[currentController_.navigationController popToRootViewControllerAnimated:YES];
}
else{
UIView *oldView = currentController_.view;
selectedIndex_ = index;
currentController_ = [viewControllers_ objectAtIndex:index];
@try {
[currentController_ setValue:self forKey:@"_parentViewController"];
}
@catch (NSException *exception) {
DLog(@"Error : The _parentViewController couldn't be set! You may have a problem");
}
@finally {
}
UIView *newView = currentController_.view;
//animate or not
[oldView removeFromSuperview];
[currentController_ viewWillAppear:NO];
[containerView_ addSubview:newView];
//executed after the view is displayed
[currentController_ performSelector:@selector(viewDidAppear:)
withObject:nil
afterDelay:0.0];
[self.view bringSubviewToFront:topBar_];
}
if (program) {
[topBar_ setSelectedItemIndex:index];
return;
}
if ([delegate_ respondsToSelector:@selector(rmTopBarController:didSelectController:atIndex:)]) {
[delegate_ rmTopBarController:self
didSelectController:currentController_
atIndex:selectedIndex_];
}
}
#pragma mark RMTopBarDelegate
- (void)rmTopBar:(RMTopBar *)topBar didSelectItemAtIndex:(NSUInteger)index{
[self selectControllerAtIndex:index programmatically:NO];
}
- (BOOL) rmTopBar:(RMTopBar *)topBar shouldSelectItemAtIndex:(NSUInteger) index{
if ([delegate_ respondsToSelector:@selector(rmTopBarController:shouldSelectViewController:)]) {
BOOL display = [delegate_ rmTopBarController:self shouldSelectViewController:index];
return display;
}
return YES;
}
#pragma mark UIViewController forward methods
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[currentController_ viewWillAppear:animated];
}
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[currentController_ viewDidAppear:animated];
}
- (void)viewDidUnload{
[topBar_ release]; topBar_ = nil;
[containerView_ release]; containerView_ = nil;
[super viewDidUnload];
}
- (void)dealloc{
[topBarXIB release];
[viewControllers_ release];
[containerView_ release];
[topBar_ release];
[super dealloc];
}
@end