UINavigationController堆栈中UIViewControllers的不同界面方向

问题描述:

我有一个纵向方向的rootviewcontroller导航控制器。然后,我想将第二个视图控制器推向横向方向的堆栈。可悲的是,我发现没有办法强制应用程序重新检查supportedInterfaceOrientations。因此,风景视图控制器将显示在照相机中,直到用户将其设备转为风景。UINavigationController堆栈中UIViewControllers的不同界面方向

我准备了一个测试项目:https://github.com/buechner/InterfaceOrientationTest

它甚至有可能到navigationcontroller堆内自动改变方向?

这是可以做到,但你将不得不作出UIStoryboardSegue子

  1. 使UIStoryBoradSegue子类,并覆盖perform()

    override func perform() { 
    let sourceVC = self.sourceViewController 
    let destonationVC = self.destinationViewController 
    
    sourceVC.navigationController!.presentViewController(destonationVC, animated: true, completion: nil) 
    } 
    
  2. 在你NavigationControllerSubclass改变shouldAutoRotatesupportedInterfaceOrientations

    public override func shouldAutorotate() -> Bool { 
    return (self.topViewController?.shouldAutorotate())! 
    } 
    
    
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
    return (self.topViewController?.supportedInterfaceOrientations())! 
    } 
    
  3. 而从viewControllers连接Segue公司选择赛格瑞子

4.增加在每类shouldAutorotatesupportedInterfaceOrientations方法

override func shouldAutorotate() -> Bool { 

    return true 
    } 

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
    return .Portrait // or landscape in case you want orientation to be landscape 
    } 
  1. 正如我们使自定义继续,您还需要添加后退按钮。
开始=>
+0

谢谢米莎。但最终,viewcontroller是模块化添加的,它不会添加到导航控制器堆栈中。所以不需要定制赛格。我可以简单地使用模态。 –

就能呈现ViewControllerLandscapeViewController强行使其landscape

使用下面按照你的LandscapeViewController

func launchLandscapeScreen() -> Void{ 

    let viewController = UIViewController() 
    self.presentViewController(viewController, animated: false, completion: nil) 
    self.dismissViewControllerAnimated(false, completion: nil) 
} 
方法在你 viewDidLoadLandscapeViewController

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

添加代码的行

+0

谢谢*。有趣的黑客。如果它只是立即改变而不是在1秒后。现在,用户在更改为预期方向之前会看到混乱的视图。 –