Skip to content

Instantly share code, notes, and snippets.

@jverkoey
Last active August 29, 2015 13:57
Show Gist options
  • Save jverkoey/9360926 to your computer and use it in GitHub Desktop.
Save jverkoey/9360926 to your computer and use it in GitHub Desktop.
Implementing UINavigationController/UITabBarController support for shouldAutorotate without subclassing or extending.
// Add this to your app delegate.
// Neither `UINavigationController` nor `UITabBarController` have the slightest decency to ask their
// visible view controller whether IT would like to rotate and just go on and do whatever the hell
// they please. This'll show 'em.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
UIViewController* topViewController = window.rootViewController;
do {
// Navigate modal controllers.
if (topViewController.presentedViewController) {
topViewController = topViewController.presentedViewController;
continue;
}
UIViewController* childViewController = nil;
if ([topViewController isKindOfClass:[UINavigationController class]]) {
childViewController = [(UINavigationController *)topViewController visibleViewController];
} else if ([topViewController isKindOfClass:[UITabBarController class]]) {
childViewController = [(UITabBarController *)topViewController selectedViewController];
}
if (childViewController == nil) {
break;
}
topViewController = childViewController;
} while (1);
if (topViewController && !topViewController.shouldAutorotate) {
// Disallow rotation - only allow the current device orientation.
return (1 << NIInterfaceOrientation());
} else if (topViewController) {
return topViewController.supportedInterfaceOrientations;
} else {
return UIInterfaceOrientationMaskAll;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment