Scofield Tran Scofield Tran - 1 month ago 13
iOS Question

Force a UIViewController just landscape in iOS 9

I have an app supports both orientation portrait and landscape. But in a viewcontroller I JUST want it present in LANDSCAPE mode. I try overriding some methods for changing orientation such as

- (BOOL)shouldAutorotate {
return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
return YES;
}
return NO;
}


But none of them are called in iOS 9. Therefore, this viewcontroller present both portrait and landscape. On contrary, it works perfectly in iOS 8.

So annoying, is there anyway to force just a viewcontroller presented in landscape mode in iOS 9

UPDATE:
As Ronak Chaniyara's answer, I solved my problem, just one controller is in landscape mode.
Now I face another problem. If I want to force one controller just in portrait mode, I implement these methods in the controller but it's still rotated if I rotate screen.

Is the solution still work with portrait mode, or I have to find another approach to force a controller just in portrait mode

Answer

I think issue will be you have defined the allowed orientations in info.plist which apparently overrides anything you do anywhere else throughout the project.

To correct the issue I removed the entries from info.plist and defined them in the project settings. Now everything works as expected.

Hope this helps.

Comments