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!!
This should work from iOS 6 on upwards, but I've only tested it on iOS 8. Subclass
UINavigationController
and override the following methods:Or ask the visible view controller
and implement the methods there.
According to Korey Hinton's answer
Swift 2.2:
Disable Rotation
Lock to Specific Orientation
According to solution showed by @sid-sha you have to put everything in the
viewDidAppear:
method, otherwise you will not get thedidRotateFromInterfaceOrientation:
fired, so something like:I have tried many solutions, but the one that worked for is the following:
There is no need to edit the
info.plist
in ios 8 and 9.Possible orientations from the Apple Documentation:
This way work for me in Swift 2 iOS 8.x:
PS (this method dont require to override orientation functions like shouldautorotate on every viewController, just one method on AppDelegate)
Check the "requires full screen" in you project general info.
So, on AppDelegate.swift make a variable:
So, put also this func:
So, in every class in your project you can set this var in viewWillAppear:
If you need to make a choices based on the device type you can do this:
Use this to lock view controller orientation, tested on IOS 9:
// Lock orientation to landscape right