Before iOS 8, we used below code in conjunction with supportedInterfaceOrientations and shouldAutoRotate delegate methods to force app orientation to any particular orientation. I used below code snippet to programmatically rotate the app to desired orientation. Firstly, I am changing the status bar orientation. And then just presenting and immediately dismissing a modal view rotates the view to desired orientation.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:vc animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
But this is failing in iOS 8. Also, I have seen some answers in stack overflow where people suggested that we should always avoid this approach from iOS 8 onwards.
To be more specific, my application is a universal type of application. There are three controllers in total.
First View controller- It should support all orientations in iPad and only portrait (home button down) in iPhone.
Second View controller- It should support only landscape right in all conditions
Third View controller- It should support only landscape right in all conditions
We are using navigation controller for page navigation. From the first view controller, on a button click action, we are pushing the second one on stack. So, when the second view controller arrives, irrespective of device orientation, the app should lock in landscape right only.
Below is my shouldAutorotate
and supportedInterfaceOrientations
methods in second and third view controller.
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(BOOL)shouldAutorotate {
return NO;
}
Is there any solution for this or any better way of locking a view controller in particular orientation for iOS 8. Please help!!
I found that if it's a presented view controller, you can override
preferredInterfaceOrientationForPresentation
Swift:
This is a feedback to comments in Sid Shah's answer, regarding how to disable animations using:
Code:
[iOS9+] If anyone dragged all the way down here as none of above solutions worked, and if you present the view you want to change orientation by segue, you might wanna check this.
Check your segue's presentation type. Mine was 'over current context'. When I changed it to Fullscreen, it worked.
Thanks to @GabLeRoux, I found this solution.
I have the same problem and waste so many time for it. So now I have my solution. My app setting is just support portrait only.However, some screens into my app need have landscape only.I fix it by have a variable isShouldRotate at AppDelegate. And the function at AppDelegate:
And finally when a ViewControllerA need landscape state. Just do that: before push/present to ViewControllerA assign isShouldRotate to true. Don't forget when pop/dismiss that controller assign isShouldRotate to false at viewWillDisappear.
First of all - this is a bad idea, in general, something wrong going with your app architecture, but,
sh..t happens
, if so, you can try to make something like below:Than, in
AppDelegate
And whenever you want to change orientation do as:
Note: Sometimes, device may not update from such call, so you may need to do as follow
That's all
On
Xcode 8
the methods are converted to properties, so the following works withSwift
: