Last active
December 13, 2015 19:09
-
-
Save jinqian/4960822 to your computer and use it in GitHub Desktop.
phonegap orientation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Declare an orientation delegate protocol somewhere */ | |
@protocol SomeOrientationDelegate <NSObject> | |
(NSUInteger)supportedInterfaceOrientations; | |
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; | |
(BOOL)shouldAutorotate; | |
@end | |
//... | |
/* Your view controller */ | |
// Implement the orientation delegate you defined | |
@interface SomeViewController : UIViewController <SomeOrientationDelegate> {} | |
@property (nonatomic, unsafe_unretained) id orientationDelegate; | |
@end | |
//... | |
// Set the orientation delegate to the MainViewController of the app (orientation controlled in the plist of Project Settings) | |
self.viewController.orientationDelegate = self.plugin.viewController; | |
// ... | |
// Implementation of the orientation protocol | |
- (BOOL)shouldAutorotate { | |
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotate)]) { | |
return [self.orientationDelegate shouldAutorotate]; | |
} | |
return YES; | |
} | |
- (NSUInteger)supportedInterfaceOrientations { | |
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) { | |
return [self.orientationDelegate supportedInterfaceOrientations]; | |
} | |
return UIInterfaceOrientationMaskPortrait; | |
} | |
// For iOS version lower than iOS6 | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { | |
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) { | |
return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation]; | |
} | |
return YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment