2

I tried to find a solution but so much information which doesn't work. My last try was using the following:

UIApplication.sharedApplication().setStatusBarOrientation(UIInterfaceOrientation.LandscapeRight, animated: false)

This however, was deprecated from iOS 9 and couldn't find any way to force rotate with UINavigationController. My app mainly uses Portrait Orientation and only one view needs to be Landscape. I need to force Landscape on one View and rest to keep as Portrait. Any help would be highly appreciated!

Some of the questions I checked are:

Setting device orientation in Swift iOS

How do I programmatically set device orientation in iOS7?

Why can't I force landscape orientation when use UINavigationController?

Community
  • 1
  • 1
Tal Zion
  • 6,308
  • 3
  • 50
  • 73

2 Answers2

3

If this is something you really want to do, subclass UINavigationController then add this code:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Landscape
}

Trying to force an orientation imperatively is unwise; it's better to tell iOS what you want (as above) then let it calculate the orientation as best it can.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71
  • 1
    Hi, thank you for your help! Unfortunately, this still doesn't work.. :/ – Tal Zion Dec 13 '15 at 01:27
  • How do you force an orientation change when pushing/popping view controllers with different orientations (e.g., A -> portrait, B -> landscape)? So popping from B->A should yield portrait while pushing A->B should yield landscape. – Crashalot Jan 04 '17 at 23:38
0

We had to do this same thing in our app as well. Initially we worked with a hack. But eventually we switched the "Landscape" VC to a modal rather than part of navigation view controller stack. I would suggest you do that. But if you really want to, here is how you do it.

Subclass Navigation VC. in supportedInterfaceOrientaions check for VC type & return appropriate orientation (landscape for one you want, portrait for rest) This itself wont autorotate that VC to landscape, so here is the hack.

In viewDidLoad/viewDidAppear of "landscape" VC, push another generic VC object & pop it subsequently

UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:c animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];

This used to work in iOS7 & we switched to modal after that. so this might now work in later versions.

GauravY
  • 78
  • 5
  • Thanks! I tried it but got an error - DeviceOrientation[4624:222192] Presenting view controllers on detached view controllers is discouraged – Tal Zion Dec 13 '15 at 18:35
  • Yes this was an issue that came up with our app too, but it was just a warning. For presenting VC as a modal you need to change the segue kind to **modal** & then `performSegueWithIdentifier` . You will need to take care of adding navigationbar yourself as this modal VC wont be a part of navigationVC stack. All other usual VC apis should return appropriate values `supportedInterfaceOrientations` should return `UIInterfaceOrientationMaskLandscape` `shouldAutorotate` should return `YES` There wont be a back button, so you should add that too in top bar. – GauravY Dec 14 '15 at 06:40