forked from grgcombs/IntelligentSplitViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntelligentSplitViewController.m
182 lines (140 loc) · 6.53 KB
/
IntelligentSplitViewController.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
//
// IntelligentSplitViewController.m
// From TexLege by Gregory S. Combs
//
// Released under the Creative Commons Attribution 3.0 Unported License
// Please see the included license page for more information.
//
// In a nutshell, you can use this, just attribute this to me in your "thank you" notes or about box.
//
#import "IntelligentSplitViewController.h"
#import <objc/message.h>
@implementation IntelligentSplitViewController
- (id) init {
if ((self = [super init])) {
NSLog(@"IntelligentSplitViewController using init: and not using a NIB.");
// I've actually never attempted to use this class without NIBs, but this should work.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willRotate:)
name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
//debug_NSLog(@"IntelligentSplitViewController awaking from a NIB: %@", self.title);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willRotate:)
name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//debug_NSLog(@"IntelligentSplitViewController loaded: %@", self.title);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
//debug_NSLog(@"IntelligentSplitViewController unloaded: %@", self.title);
[super viewDidUnload];
}
- (void)dealloc {
@try {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@catch (NSException * e) {
NSLog(@"IntelligentSplitViewController DE-OBSERVING CRASHED: %@ ... error:%@", self.title, [e description]);
}
[super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)willRotate:(id)sender {
if (![self isViewLoaded]) // we haven't even loaded up yet, let's turn away from this place
return;
NSNotification *notification = sender;
if (!notification)
return;
UIInterfaceOrientation toOrientation = [[notification.userInfo valueForKey:UIApplicationStatusBarOrientationUserInfoKey] integerValue];
//UIInterfaceOrientation fromOrientation = [UIApplication sharedApplication].statusBarOrientation;
UITabBarController *tabBar = self.tabBarController;
BOOL notModal = (!tabBar.modalViewController);
BOOL isSelectedTab = [self.tabBarController.selectedViewController isEqual:self];
NSTimeInterval duration = [[UIApplication sharedApplication] statusBarOrientationAnimationDuration];
if (!isSelectedTab || !notModal) {
// Looks like we're not "visible" ... propogate rotation info
[super willRotateToInterfaceOrientation:toOrientation duration:duration];
UIViewController *master = [self.viewControllers objectAtIndex:0];
NSObject *theDelegate = (NSObject *)self.delegate;
#define YOU_DONT_FEEL_QUEAZY_ABOUT_THIS_BECAUSE_IT_PASSES_THE_APP_STORE 1
#if YOU_DONT_FEEL_QUEAZY_ABOUT_THIS_BECAUSE_IT_PASSES_THE_APP_STORE
UIBarButtonItem *button = [super valueForKey:@"_barButtonItem"];
#else //YOU_DO_FEEL_QUEAZY_AND_FOR_SOME_REASON_YOU_PREFER_THE_LESSER_EVIL_____FRIGHTENING_STUFF
UIBarButtonItem *button = [[[[[self.viewControllers objectAtIndex:1]
viewControllers] objectAtIndex:0]
navigationItem] rightBarButtonItem];
#endif
if (UIInterfaceOrientationIsPortrait(toOrientation)) {
if (theDelegate && [theDelegate respondsToSelector:@selector(splitViewController:willHideViewController:withBarButtonItem:forPopoverController:)]) {
@try {
UIPopoverController *popover = [super valueForKey:@"_hiddenPopoverController"];
objc_msgSend(theDelegate, @selector(splitViewController:willHideViewController:withBarButtonItem:forPopoverController:), self, master, button, popover);
}
@catch (NSException * e) {
NSLog(@"There was a nasty error while notifyng splitviewcontrollers of an orientation change: %@", [e description]);
}
}
}
else if (UIInterfaceOrientationIsLandscape(toOrientation)) {
if (theDelegate && [theDelegate respondsToSelector:@selector(splitViewController:willShowViewController:invalidatingBarButtonItem:)]) {
@try {
objc_msgSend(theDelegate, @selector(splitViewController:willShowViewController:invalidatingBarButtonItem:), self, master, button);
}
@catch (NSException * e) {
NSLog(@"There was a nasty error while notifyng splitviewcontrollers of an orientation change: %@", [e description]);
}
}
}
}
//debug_NSLog(@"MINE WillRotate ---- sender = %@ to = %d from = %d", [sender class], toOrientation, fromOrientation);
}
/*
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
//debug_NSLog(@"Theirs --- will rotate");
}
*/
- (void)didRotate:(id)sender {
if (![self isViewLoaded]) // we haven't even loaded up yet, let's turn away from this place
return;
NSNotification *notification = sender;
if (!notification)
return;
UIInterfaceOrientation fromOrientation = [[notification.userInfo valueForKey:UIApplicationStatusBarOrientationUserInfoKey] integerValue];
//UIInterfaceOrientation toOrientation = [UIApplication sharedApplication].statusBarOrientation;
UITabBarController *tabBar = self.tabBarController;
BOOL notModal = (!tabBar.modalViewController);
BOOL isSelectedTab = [self.tabBarController.selectedViewController isEqual:self];
if (!isSelectedTab || !notModal) {
// Looks like we're not "visible" ... propogate rotation info
[super didRotateFromInterfaceOrientation:fromOrientation];
}
//debug_NSLog(@"MINE DidRotate ---- sender = %@ from = %d to = %d", [sender class], fromOrientation, toOrientation);
}
/*
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
//debug_NSLog(@"Theirs --- did rotate");
}
*/
@end