2
votes

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?

2
did you find a solution?Crashalot

2 Answers

3
votes

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.

0
votes

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.