ViewController Orientation Lock

I have an application that supports all kinds of orientations, however I want certain viewControllers to support only the landscape and several to block the Potrait mode. How can I do this, I tried to execute the code below, but nothing works.

- (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } // pre-iOS 6 support - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationPortrait); } 
+4
source share
1 answer

All view controllers connected to the same navigation controller must support the same orientation. In particular, I believe that only the root view controller shouldAutorotateToInterfaceOrientation taken into account. You can take a look at this SO post for a kind of workaround; or this one for another approach that might help.

The second post I linked above suggests using this definition for shouldAutorotateToInterfaceOrientation , you can customize it for your case:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) { UIViewController *rootController = [((UINavigationController *)self.selectedViewController).viewControllers objectAtIndex:0]; return [rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; } return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; } 
+3
source

All Articles