2
votes

I have a navigation controller with a rootviewcontroller with portrait orientation. Then i want to push a second viewcontroller to the stack with landscape orientation. Sadly i found no way to force the app to recheck supportedInterfaceOrientations. So the landscape viewcontroller is shown in protrait until the user rotates his device into landscape.

I prepared a test project: https://github.com/buechner/InterfaceOrientationTest

Is it even possible to automatically change the orientation within a navigationcontroller stack?

2

2 Answers

2
votes

You can present a ViewController within LandscapeViewController to forcefully make it landscape

Use following line of the code in your viewDidLoad of LandscapeViewController

self.performSelector(#selector(LandscapeViewController.launchLandscapeScreen), withObject: nil, afterDelay:1)

add following method in your LandscapeViewController

func launchLandscapeScreen() -> Void{

    let viewController = UIViewController()
    self.presentViewController(viewController, animated: false, completion: nil)
    self.dismissViewControllerAnimated(false, completion: nil)
}
0
votes

It could be done but you'll have to make UIStoryboardSegue subclass

  1. Make a subclass of UIStoryBoradSegue and override perform()

    override func perform() {
    let sourceVC = self.sourceViewController
    let destonationVC =  self.destinationViewController
    
    sourceVC.navigationController!.presentViewController(destonationVC, animated: true, completion: nil)
    }
    
  2. In your NavigationControllerSubclass change shouldAutoRotate and supportedInterfaceOrientations

    public override func shouldAutorotate() -> Bool {
    return (self.topViewController?.shouldAutorotate())!
    }
    
    
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return (self.topViewController?.supportedInterfaceOrientations())!
    }
    
  3. while connecting Segue from viewControllers select your segue subclass

4.add shouldAutorotate and supportedInterfaceOrientations methods in every class to

    override func shouldAutorotate() -> Bool {

    return true
     }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Portrait // or landscape in case you want orientation to be landscape
    }
  1. As we're making custom segue, you also need to add back button.