Reputation: 3590
I want to block the landscape orientation for some UIViewController
. I read a lot of different thing to manage it, but I didn't find a solution working well. Every time, the orientation isn't blocked even if it should be.
I have the following code to block the landscape orientation in the UIViewController
which doesn't have the landscape rotation:
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
Even with that, the view is rotating in landscape.
Did I miss something?
Upvotes: 0
Views: 103
Reputation: 1272
Do you have your UIViewController
inside a UINavigationController
? Or TabBarController
? In that case, the UIViewController
just passes that call to the UINavigationController
and you will have to subclass UINavigationController
and implement these methods there and check which of the view controllers in the navigation controller's stack is the one that wants to rotate and only allow the rotation for the one that you want.
This is how I did it in a project:
- (NSUInteger)supportedInterfaceOrientations
{
if ([self.visibleViewController class] == [MyViewController class]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
You can as well try to ask the UIViewController in question from the navigation controller, but this didn't work for me well for some reason.
- (NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
You use the same approach for the rest of the methods as well.
Upvotes: 1