Skip to content

Instantly share code, notes, and snippets.

@brunojppb
Created January 23, 2017 15:24
Show Gist options
  • Save brunojppb/3f551b2a4b0b0215ec07e524bc1cfb09 to your computer and use it in GitHub Desktop.
Save brunojppb/3f551b2a4b0b0215ec07e524bc1cfb09 to your computer and use it in GitHub Desktop.
Allow landscape mode in specific ViewControllers
/* Allow Landscape mode for specific ViewControllers */
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
UIViewController* topVC = [self topViewControllerWith: self.window.rootViewController];
if ([topVC respondsToSelector:@selector(canRotate)]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
/* get the top ViewController */
- (UIViewController*) topViewControllerWith:(UIViewController *)rootViewController {
if (rootViewController == nil) { return nil; }
if ([rootViewController isKindOfClass: [UITabBarController class]]) {
return [self topViewControllerWith: ((UITabBarController*) rootViewController).selectedViewController];
}
else if ([rootViewController isKindOfClass: [UINavigationController class]]) {
return [self topViewControllerWith: ((UINavigationController*) rootViewController).visibleViewController];
}
else if (rootViewController.presentedViewController != nil) {
return [self topViewControllerWith: [rootViewController presentedViewController]];
}
return rootViewController;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment