-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAPSplitViewController.m
More file actions
executable file
·273 lines (212 loc) · 10.9 KB
/
APSplitViewController.m
File metadata and controls
executable file
·273 lines (212 loc) · 10.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
//
// APBaseSplitViewController.m
// SplitSample
//
// Created by slatvick on 2/15/11.
// Copyright 2011 Alterplay. All rights reserved.
//
#import "APSplitViewController.h"
#import <QuartzCore/QuartzCore.h>
#define constDividerWidth 0.0f
#define constMasterWidth 320.0f
#define constDetailStartPoint (constMasterWidth+constDividerWidth)
static float koefficient = 0.0f; // Need to fix a bug with 20px near the statusbar
@interface APSplitViewController()
//- (void) layoutSubviews;
@end
@implementation APSplitViewController
#pragma mark -
#pragma mark Helpers
// returns size of split view
- (CGSize) sizeRotated {
UIScreen *screen = [UIScreen mainScreen];
CGRect bounds = screen.bounds; // always implicitly in Portrait orientation.
CGRect appFrame = screen.applicationFrame;
CGSize size = bounds.size;
float statusBarHeight = MAX((bounds.size.width - appFrame.size.width), (bounds.size.height - appFrame.size.height));
// let's figure out if width/height must be swapped
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
// we're going to landscape, which means we gotta swap them
size.width = bounds.size.height;
size.height = bounds.size.width;
}
size.height = size.height -statusBarHeight -self.tabBarController.tabBar.frame.size.height;
return size;
}
- (void) layoutSubviews {
// NSLog(@"layoutSubviews");
CGSize size = [self sizeRotated];
_masterContainerView.frame = CGRectMake(0, 0 - koefficient, constMasterWidth, size.height + koefficient);
self.detailViewController.view.frame = CGRectMake(constDetailStartPoint, 0 - koefficient, size.width - constDetailStartPoint, size.height + koefficient);
}
#pragma mark - View lifecycle
/*- (void) pushToMasterController:(UIViewController *)controller {
CGSize size = [self sizeRotated];
controller.view.frame = CGRectMake(0, 0, constMasterWidth, size.height);
controller.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
// controller.view.backgroundColor = [UIColor redColor];
[_master pushViewController:controller animated:NO];
_master.view.frame = CGRectMake(0, 0, constMasterWidth, size.height);
_master.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_master.view.backgroundColor = [UIColor purpleColor];
[self.view addSubview:_master.view];
[self.view bringSubviewToFront:_divider];
}
*/
- (void)setMasterViewController:(UIViewController *)master
{
if (self.masterViewController) {
[self.masterViewController.view removeFromSuperview];
[self.masterViewController removeFromParentViewController];
}
_masterViewController = master;
CGSize size = [self sizeRotated];
self.masterViewController.view.frame = CGRectMake(0, 0, constMasterWidth, size.height);
_masterContainerView.layer.shadowColor = [UIColor blackColor].CGColor;
_masterContainerView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
_masterContainerView.layer.shadowOpacity = 1.0;
_masterContainerView.layer.shadowRadius = 5.0;
CGPathRef shadowPath = CGPathCreateWithRect(_masterContainerView.frame, NULL);
_masterContainerView.layer.shadowPath = shadowPath;
CGPathRelease(shadowPath);
_masterContainerView.clipsToBounds = NO;
self.masterViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self addChildViewController:self.masterViewController];
[_masterContainerView addSubview:self.masterViewController.view];
[self.masterViewController didMoveToParentViewController:self];
}
- (void)setDetailViewController:(UIViewController *)detail
{
if (self.detailViewController) {
[self.detailViewController.view removeFromSuperview];
[self.detailViewController removeFromParentViewController];
}
_detailViewController = detail;
CGSize size = [self sizeRotated];
self.detailViewController.view.frame = CGRectMake(0, 0, size.width-constDetailStartPoint, size.height);
self.detailViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
//[self.detail pushViewController:controller animated:NO];
self.detailViewController.view.frame = CGRectMake(constDetailStartPoint, 0, size.width-constDetailStartPoint, size.height);
self.detailViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
// _detail.view.backgroundColor = [UIColor darkGrayColor];
[self addChildViewController:self.detailViewController];
[self.view addSubview:self.detailViewController.view];
[self.view bringSubviewToFront:_masterContainerView];
}
/*
- (void) pushToDetailController:(UIViewController *)controller {
CGSize size = [self sizeRotated];
controller.view.frame = CGRectMake(0, 0, size.width-constDetailStartPoint, size.height);
controller.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
// controller.view.backgroundColor = [UIColor lightGrayColor];
[_detail pushViewController:controller animated:NO];
_detail.view.frame = CGRectMake(constDetailStartPoint, 0, size.width-constDetailStartPoint, size.height);
_detail.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
// _detail.view.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:_detail.view];
[self.view bringSubviewToFront:_divider];
}
*/
- (void) loadView {
CGSize size = [self sizeRotated];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view = view;
self.view.backgroundColor = [UIColor grayColor];
// slatvick: old divider
/* _divider = [[UIView alloc] init];
_divider.frame = CGRectMake(constMasterWidth, 0, constDividerWidth, size.height);
_divider.backgroundColor = [UIColor redColor];
_divider.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:_divider];
UIImage *image = [UIImage imageNamed:@"split-divider.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(-4, 0, image.size.width, image.size.height);
[_divider addSubview:imageView];
*/
}
- (void) viewDidLoad {
[super viewDidLoad];
CGSize size = [self sizeRotated];
_masterContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 - koefficient, constMasterWidth, size.height + koefficient)];
[self.view addSubview:_masterContainerView];
//_master = [[APNavigationControllerForSplitController alloc] initWithSplit:self];
//_detail = [[APNavigationControllerForSplitController alloc] initWithSplit:self];
// _master = [[UINavigationController alloc] init];
// _detail = [[UINavigationController alloc] init];
// don't remember what for?
//[_master viewDidLoad];
//[_detail viewDidLoad];
// NSLog(@" self.view.frame: %@", NSStringFromCGRect(self.view.frame));
// NSLog(@"_master.view.frame: %@", NSStringFromCGRect(_master.view.frame));
// NSLog(@"_detail.view.frame: %@", NSStringFromCGRect(_detail.view.frame));
}
- (void) viewDidUnload {
self.masterViewController = nil;
self.detailViewController = nil;
}
#pragma mark -
#pragma mark STANDARD METHODS
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.masterViewController viewWillAppear:animated];
[self.detailViewController viewWillAppear:animated];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.masterViewController viewDidAppear:animated];
[self.detailViewController viewDidAppear:animated];
// it's Needed to fix a bug with 20px near the statusbar
if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
koefficient = 20.0f;
else
koefficient = 0.0f;
// [self layoutSubviews];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.masterViewController viewWillDisappear:animated];
[self.detailViewController viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.masterViewController viewDidDisappear:animated];
[self.detailViewController viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self.masterViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.detailViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
// [self layoutSubviews];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.masterViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[self.detailViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
// [self layoutSubviews];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self.masterViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.detailViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self.masterViewController willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.detailViewController willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
[self.masterViewController didAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation];
[self.detailViewController didAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation];
}
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
[self.masterViewController willAnimateSecondHalfOfRotationFromInterfaceOrientation:fromInterfaceOrientation duration:duration];
[self.detailViewController willAnimateSecondHalfOfRotationFromInterfaceOrientation:fromInterfaceOrientation duration:duration];
}
@end