Skip to content

Instantly share code, notes, and snippets.

@jinqian
Last active December 13, 2015 19:09
Show Gist options
  • Save jinqian/4960822 to your computer and use it in GitHub Desktop.
Save jinqian/4960822 to your computer and use it in GitHub Desktop.
phonegap orientation
/* 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