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 is what worked for me:
https://developer.apple.com/library//ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/clm/UIViewController/attemptRotationToDeviceOrientation
Call it in your viewDidAppear: method.
The combination of Sids and Koreys answers worked for me.
Extending the Navigation Controller:
Then disabling rotation on the single View
And rotating to the appropriate orientation before the segue
The top solution above:
didnt'work for me when I called it in viewDidAppear of the presented view controller. However it did work when I called it in preparForSegue in the presenting view controller.
(Sorry, not enough reputation points to comment on that solution, so I had to add it like this)
My requirements are
AVPlayerViewController
to play videoWhen video is playing, if it's a landscape then allow the screen to rotate landscape right and landscape left. If it's a portrait then lock the view in portrait mode only.
First, define
supportedInterfaceOrientationsForWindow
inAppDelegate.swift
Second, in your main view controller, define following functions
Then, you need to subclass
AVPlayerViewController
Override these three functions in
MyPlayerViewController.swift
Because user might rotate device landscape left or landscape right, we need to set auto rotate to be true
Finally, create
MyPlayerViewController
instance in your view controller and set the property size value.Initiate your player with proper
videoUrl
, then assign your player toplayerViewController
. Happy coding!There still seems to be some debate about how best to accomplish this task, so I thought I'd share my (working) approach. Add the following code in your
UIViewController
implementation:For this example, you will also need to set your allowed device orientations to 'Landscape Left' in your project settings (or directly in
info.plist
). Just change the specific orientation you want to force if you want something other thanLandscapeLeft.
The key for me was the
attemptRotationToDeviceOrientation
call inviewWillAppear
- without that the view would not properly rotate without physically rotating the device.