Skip to content

Commit 7f79d9a

Browse files
committed
refactor window related code to SISecondaryWindowRootViewController
1 parent 908b491 commit 7f79d9a

4 files changed

Lines changed: 100 additions & 54 deletions

File tree

SIAlertView/SIAlertView.m

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
#import "SIAlertView.h"
10-
#import "UIWindow+SIUtils.h"
10+
#import "SISecondaryWindowRootViewController.h"
1111
#import <QuartzCore/QuartzCore.h>
1212

1313
NSString *const SIAlertViewWillShowNotification = @"SIAlertViewWillShowNotification";
@@ -140,7 +140,7 @@ @implementation SIAlertItem
140140

141141
#pragma mark - SIAlertViewController
142142

143-
@interface SIAlertViewController : UIViewController
143+
@interface SIAlertViewController : SISecondaryWindowRootViewController
144144

145145
@property (nonatomic, strong) SIAlertView *alertView;
146146

@@ -163,62 +163,11 @@ - (void)viewDidLoad
163163

164164
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
165165
{
166+
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
166167
[self.alertView resetTransition];
167168
[self.alertView invalidateLayout];
168169
}
169170

170-
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
171-
{
172-
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
173-
[self setNeedsStatusBarAppearanceUpdate];
174-
}
175-
}
176-
177-
- (NSUInteger)supportedInterfaceOrientations
178-
{
179-
UIViewController *viewController = [self.alertView.oldKeyWindow currentViewController];
180-
if (viewController) {
181-
return [viewController supportedInterfaceOrientations];
182-
}
183-
return UIInterfaceOrientationMaskAll;
184-
}
185-
186-
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
187-
{
188-
UIViewController *viewController = [self.alertView.oldKeyWindow currentViewController];
189-
if (viewController) {
190-
return [viewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
191-
}
192-
return YES;
193-
}
194-
195-
- (BOOL)shouldAutorotate
196-
{
197-
UIViewController *viewController = [self.alertView.oldKeyWindow currentViewController];
198-
if (viewController) {
199-
return [viewController shouldAutorotate];
200-
}
201-
return YES;
202-
}
203-
204-
- (UIStatusBarStyle)preferredStatusBarStyle
205-
{
206-
UIWindow *window = self.alertView.oldKeyWindow;
207-
if (!window) {
208-
window = [UIApplication sharedApplication].windows[0];
209-
}
210-
return [[window viewControllerForStatusBarStyle] preferredStatusBarStyle];
211-
}
212-
213-
- (BOOL)prefersStatusBarHidden
214-
{
215-
UIWindow *window = self.alertView.oldKeyWindow;
216-
if (!window) {
217-
window = [UIApplication sharedApplication].windows[0];
218-
}
219-
return [[window viewControllerForStatusBarHidden] prefersStatusBarHidden];
220-
}
221-
222171
@end
223172

224173
#pragma mark - SIAlertView
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// SISecondaryWindowRootViewController.h
3+
//
4+
// Created by Kevin Cao on 13-11-9.
5+
// Copyright (c) 2013年 Sumi Interactive. All rights reserved.
6+
//
7+
8+
#import <UIKit/UIKit.h>
9+
10+
/**
11+
* This view controller is supposed to be set as secondary window's root view controller.
12+
* It transfer the responsibility of controlling rotation and status bar to the main window's current view controller.
13+
*/
14+
@interface SISecondaryWindowRootViewController : UIViewController
15+
16+
@end
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// SIFloatWindowRootViewController.m
3+
//
4+
// Created by Kevin Cao on 13-11-9.
5+
// Copyright (c) 2013年 Sumi Interactive. All rights reserved.
6+
//
7+
8+
#import "SISecondaryWindowRootViewController.h"
9+
#import "UIWindow+SIUtils.h"
10+
11+
#define MAIN_WINDOW [UIApplication sharedApplication].windows[0]
12+
13+
@interface SISecondaryWindowRootViewController ()
14+
15+
@end
16+
17+
@implementation SISecondaryWindowRootViewController
18+
19+
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
20+
{
21+
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
22+
23+
#ifdef __IPHONE_7_0
24+
// give the current view controller in charge a chance to update status bar
25+
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
26+
[self setNeedsStatusBarAppearanceUpdate];
27+
}
28+
#endif
29+
}
30+
31+
#pragma mark - Rotation
32+
33+
- (NSUInteger)supportedInterfaceOrientations
34+
{
35+
UIViewController *viewController = [MAIN_WINDOW currentViewController];
36+
if (viewController) {
37+
return [viewController supportedInterfaceOrientations];
38+
}
39+
return UIInterfaceOrientationMaskAll;
40+
}
41+
42+
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
43+
{
44+
UIViewController *viewController = [MAIN_WINDOW currentViewController];
45+
if (viewController) {
46+
return [viewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
47+
}
48+
return YES;
49+
}
50+
51+
- (BOOL)shouldAutorotate
52+
{
53+
UIViewController *viewController = [MAIN_WINDOW currentViewController];
54+
if (viewController) {
55+
return [viewController shouldAutorotate];
56+
}
57+
return YES;
58+
}
59+
60+
#ifdef __IPHONE_7_0
61+
62+
#pragma mark - Status Bar
63+
64+
- (UIStatusBarStyle)preferredStatusBarStyle
65+
{
66+
return [[MAIN_WINDOW viewControllerForStatusBarStyle] preferredStatusBarStyle];
67+
}
68+
69+
- (BOOL)prefersStatusBarHidden
70+
{
71+
return [[MAIN_WINDOW viewControllerForStatusBarHidden] prefersStatusBarHidden];
72+
}
73+
#endif
74+
75+
@end

SIAlertViewExample/SIAlertViewExample.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
7E23C6A31732776100784CF1 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 7E23C6A21732776100784CF1 /* [email protected] */; };
3838
7E23C6A8173277EA00784CF1 /* Icon-72.png in Resources */ = {isa = PBXBuildFile; fileRef = 7E23C6A7173277EA00784CF1 /* Icon-72.png */; };
3939
7E23C6AA173277EE00784CF1 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 7E23C6A9173277EE00784CF1 /* [email protected] */; };
40+
7E5D24C318ADF36F00B21A14 /* SISecondaryWindowRootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7E5D24C218ADF36F00B21A14 /* SISecondaryWindowRootViewController.m */; };
4041
7EEECA69182350B1001FEDF5 /* UIWindow+SIUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EEECA68182350B1001FEDF5 /* UIWindow+SIUtils.m */; };
4142
/* End PBXBuildFile section */
4243

@@ -77,6 +78,8 @@
7778
7E23C6A21732776100784CF1 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
7879
7E23C6A7173277EA00784CF1 /* Icon-72.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-72.png"; sourceTree = "<group>"; };
7980
7E23C6A9173277EE00784CF1 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
81+
7E5D24C118ADF36F00B21A14 /* SISecondaryWindowRootViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SISecondaryWindowRootViewController.h; sourceTree = "<group>"; };
82+
7E5D24C218ADF36F00B21A14 /* SISecondaryWindowRootViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SISecondaryWindowRootViewController.m; sourceTree = "<group>"; };
8083
7EEECA67182350B1001FEDF5 /* UIWindow+SIUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWindow+SIUtils.h"; sourceTree = "<group>"; };
8184
7EEECA68182350B1001FEDF5 /* UIWindow+SIUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWindow+SIUtils.m"; sourceTree = "<group>"; };
8285
/* End PBXFileReference section */
@@ -175,6 +178,8 @@
175178
children = (
176179
7E23C69A17325EBD00784CF1 /* SIAlertView.h */,
177180
7E23C69B17325EBD00784CF1 /* SIAlertView.m */,
181+
7E5D24C118ADF36F00B21A14 /* SISecondaryWindowRootViewController.h */,
182+
7E5D24C218ADF36F00B21A14 /* SISecondaryWindowRootViewController.m */,
178183
7EEECA67182350B1001FEDF5 /* UIWindow+SIUtils.h */,
179184
7EEECA68182350B1001FEDF5 /* UIWindow+SIUtils.m */,
180185
);
@@ -298,6 +303,7 @@
298303
buildActionMask = 2147483647;
299304
files = (
300305
7E23C67F17325EAA00784CF1 /* main.m in Sources */,
306+
7E5D24C318ADF36F00B21A14 /* SISecondaryWindowRootViewController.m in Sources */,
301307
7E23C68317325EAA00784CF1 /* AppDelegate.m in Sources */,
302308
7E23C69217325EAB00784CF1 /* ViewController.m in Sources */,
303309
7E23C69D17325EBD00784CF1 /* SIAlertView.m in Sources */,

0 commit comments

Comments
 (0)