Skip to content

Instantly share code, notes, and snippets.

@sdpjswl
Last active August 29, 2015 14:06
Show Gist options
  • Save sdpjswl/584f4ade25e77866a27a to your computer and use it in GitHub Desktop.
Save sdpjswl/584f4ade25e77866a27a to your computer and use it in GitHub Desktop.
Limit orientation to one ViewController
// implement the following code in AppDelegate.m
// implement a dummy method "canRotate" in the ViewController that you want to rotate
#pragma mark - Rotation methods
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
// Get topmost/visible view controller
UIViewController *currentViewController = [self topViewController];
// Check whether it implements a dummy methods called canRotate
if ([currentViewController respondsToSelector:@selector(canRotate)]) {
// Unlock landscape view orientations for this view controller
return UIInterfaceOrientationMaskAllButUpsideDown;
}
// Only allow portrait (standard behaviour)
return UIInterfaceOrientationMaskPortrait;
}
-(UIViewController *)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
-(UIViewController *)topViewControllerWithRootViewController:(UIViewController *)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
}
else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
}
else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
}
else if ([rootViewController isKindOfClass:[MMDrawerController class]]) {
MMDrawerController *drawer = (MMDrawerController *)rootViewController;
if (drawer.openSide == MMDrawerSideLeft) {
return drawer.leftDrawerViewController;
}
else if (drawer.openSide == MMDrawerSideNone){
return drawer.centerViewController;
}
else {
return drawer.rightDrawerViewController;
}
}
else {
return rootViewController;
}
}
-(void)canRotate {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment