Tal Zion
Tal Zion

Reputation: 6526

How to force set Landscape Orientation with UINavigationController (Swift 2.0 iOS 9)

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?

Upvotes: 2

Views: 3187

Answers (2)

GauravY
GauravY

Reputation: 78

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.

Upvotes: 0

TwoStraws
TwoStraws

Reputation: 13127

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.

Upvotes: 3

Related Questions