forked from jcmendez/JCMSegmentPageController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JCMSegmentPageController.m
260 lines (210 loc) · 9.7 KB
/
JCMSegmentPageController.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
/*
Copyright 2012 Juan-Carlos Mendez ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "JCMSegmentPageController.h"
static const float TAB_BAR_HEIGHT = 44.0f;
@implementation JCMSegmentPageController {
UIView *headerContainerView;
UISegmentedControl *segmentedControl;
UIView *contentContainerView;
}
@synthesize viewControllers = _viewControllers;
@synthesize selectedIndex = _selectedIndex;
@synthesize delegate = _delegate;
- (void)removeAllSegments {
[segmentedControl removeAllSegments];
}
- (void)addSegments {
NSUInteger index = 0;
for (UIViewController *viewController in self.viewControllers) {
[segmentedControl insertSegmentWithTitle:viewController.title atIndex:index animated:NO];
++index;
}
}
- (void)reloadTabButtons {
[self removeAllSegments];
[self addSegments];
// TODO -- Do I need this???
NSUInteger lastIndex = _selectedIndex;
_selectedIndex = NSNotFound;
self.selectedIndex = lastIndex;
}
- (void)layoutHeaderView {
CGRect rect = CGRectMake(0, 0, self.view.bounds.size.width, TAB_BAR_HEIGHT);
segmentedControl.frame = CGRectInset(rect, 5.0, 5.0);
}
/**
* When the view loads, we set the header, and on it, the segmented control that will control
* the page being displayed
*/
- (void)viewDidLoad {
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
CGRect rect = CGRectMake(0, 0, self.view.bounds.size.width, TAB_BAR_HEIGHT);
headerContainerView = [[UIView alloc] initWithFrame:rect];
headerContainerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
CGRect segmentedControlRect = CGRectInset(rect, 5.0, 5.0);
segmentedControl = [[UISegmentedControl alloc] initWithFrame:segmentedControlRect];
segmentedControl.momentary = NO;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:@selector(tabButtonPressed:) forControlEvents:UIControlEventValueChanged];
[headerContainerView addSubview:segmentedControl];
[self.view addSubview:headerContainerView];
rect.origin.y = TAB_BAR_HEIGHT;
rect.size.height = self.view.bounds.size.height - TAB_BAR_HEIGHT;
contentContainerView = [[UIView alloc] initWithFrame:rect];
contentContainerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:contentContainerView];
[self reloadTabButtons];
}
- (void)viewDidUnload {
[super viewDidUnload];
headerContainerView = nil;
contentContainerView = nil;
segmentedControl = nil;
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self layoutHeaderView];
}
- (void)dealloc {
_viewControllers = nil;
_delegate = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Only rotate if all child view controllers agree on the new orientation.
for (UIViewController *viewController in self.viewControllers) {
if (![viewController shouldAutorotateToInterfaceOrientation:interfaceOrientation])
return NO;
}
return YES;
}
- (void)setViewControllers:(NSArray *)newViewControllers {
NSAssert([newViewControllers count] >= 2, @"JCMSegmentPageController requires at least two view controllers");
UIViewController *oldSelectedViewController = self.selectedViewController;
if([UIViewController instancesRespondToSelector:@selector(willMoveToParentViewController:)])
{
// Remove the old child view controllers.
for (UIViewController *viewController in _viewControllers) {
[viewController willMoveToParentViewController:nil];
[viewController removeFromParentViewController];
}
}
_viewControllers = [newViewControllers copy];
// This follows the same rules as UITabBarController for trying to
// re-select the previously selected view controller.
NSUInteger newIndex = [_viewControllers indexOfObject:oldSelectedViewController];
if (newIndex != NSNotFound)
_selectedIndex = newIndex;
else if (newIndex < [_viewControllers count])
_selectedIndex = newIndex;
else
_selectedIndex = 0;
if([self respondsToSelector:@selector(addChildViewController:)])
{
// Add the new child view controllers.
for (UIViewController *viewController in _viewControllers) {
[self addChildViewController:viewController];
if([viewController respondsToSelector:@selector(didMoveToParentViewController:)])
[viewController didMoveToParentViewController:self];
}
}
if ([self isViewLoaded])
[self reloadTabButtons];
}
- (void)setSelectedIndex:(NSUInteger)newSelectedIndex {
[self setSelectedIndex:newSelectedIndex animated:NO];
}
- (void)setSelectedIndex:(NSUInteger)newSelectedIndex animated:(BOOL)animated {
NSAssert(newSelectedIndex < [self.viewControllers count], @"View controller index out of bounds");
if ([self.delegate respondsToSelector:@selector(segmentPageController:shouldSelectViewController:atIndex:)]) {
UIViewController *toViewController = [self.viewControllers objectAtIndex:newSelectedIndex];
if (![self.delegate segmentPageController:self shouldSelectViewController:toViewController atIndex:newSelectedIndex])
return;
}
if (![self isViewLoaded]) {
_selectedIndex = newSelectedIndex;
}
else if (_selectedIndex != newSelectedIndex) {
UIViewController *fromViewController = nil;
UIViewController *toViewController = nil;
NSUInteger oldSelectedIndex = _selectedIndex;
_selectedIndex = newSelectedIndex;
if (_selectedIndex != NSNotFound) {
[segmentedControl setSelectedSegmentIndex:_selectedIndex];
toViewController = self.selectedViewController;
}
if (toViewController == nil) { // don't animate
[fromViewController.view removeFromSuperview];
}
else if (fromViewController == nil) { // don't animate
toViewController.view.frame = contentContainerView.bounds;
[contentContainerView addSubview:toViewController.view];
if ([self.delegate respondsToSelector:@selector(segmentPageController:didSelectViewController:atIndex:)])
[self.delegate segmentPageController:self didSelectViewController:toViewController atIndex:newSelectedIndex];
} else if (animated) {
CGRect rect = contentContainerView.bounds;
if (oldSelectedIndex < newSelectedIndex)
rect.origin.x = rect.size.width;
else
rect.origin.x = -rect.size.width;
toViewController.view.frame = rect;
headerContainerView.userInteractionEnabled = NO;
if([self respondsToSelector:@selector(transitionFromViewController:toViewController:duration:options:animations:completion:)])
{
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:0.3
options:UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionCurveEaseOut
animations:^ {
CGRect rect = fromViewController.view.frame;
if (oldSelectedIndex < newSelectedIndex)
rect.origin.x = -rect.size.width;
else
rect.origin.x = rect.size.width;
fromViewController.view.frame = rect;
toViewController.view.frame = contentContainerView.bounds;
}
completion:^(BOOL finished) {
headerContainerView.userInteractionEnabled = YES;
if ([self.delegate respondsToSelector:@selector(segmentPageController:didSelectViewController:atIndex:)])
[self.delegate segmentPageController:self didSelectViewController:toViewController atIndex:newSelectedIndex];
}];
}
} else { // not animated
[fromViewController.view removeFromSuperview];
toViewController.view.frame = contentContainerView.bounds;
[contentContainerView addSubview:toViewController.view];
if ([self.delegate respondsToSelector:@selector(segmentPageController:didSelectViewController:atIndex:)])
[self.delegate segmentPageController:self didSelectViewController:toViewController atIndex:newSelectedIndex];
}
}
}
- (UIViewController *)selectedViewController {
if (self.selectedIndex != NSNotFound)
return [self.viewControllers objectAtIndex:self.selectedIndex];
else
return nil;
}
- (void)setSelectedViewController:(UIViewController *)newSelectedViewController {
[self setSelectedViewController:newSelectedViewController animated:NO];
}
- (void)setSelectedViewController:(UIViewController *)newSelectedViewController animated:(BOOL)animated;
{
NSUInteger index = [self.viewControllers indexOfObject:newSelectedViewController];
if (index != NSNotFound)
[self setSelectedIndex:index animated:animated];
}
- (void)tabButtonPressed:(UISegmentedControl *)sender {
[self setSelectedIndex:sender.selectedSegmentIndex animated:YES];
}
@end