如何使用 UINavigationController 强制设置横向方向 (Swift 2.0 iOS 9)

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

我试图找到解决方案,但没有找到太多信息。我最后一次尝试使用以下内容:

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

然而,这已从 iOS 9 中弃用,并且找不到任何方法来强制旋转 UINavigationController。我的应用程序主要使用纵向,只有一个视图需要是横向的。我需要在一个视图上强制横向,然后休息以保持纵向。任何帮助将不胜感激!

我检查的一些问题是:

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?

如果这是您真正想要做的事情,子类化 UINavigationController 然后添加此代码:

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

试图强加一个方向是不明智的;最好告诉 iOS 你想要什么(如上所述),然后让它尽可能地计算方向。

我们也必须在我们的应用程序中做同样的事情。最初我们与黑客合作。但最终我们将 "Landscape" VC 切换为模式而不是导航视图控制器堆栈的一部分。我建议你这样做。但如果你真的想要,这里是你如何做的。

子类导航VC。 在 supportedInterfaceOrientaions 中检查 VC 类型 & return 适当的方向(你想要的横向,其余的纵向) 这本身不会将 VC 自动旋转到横向,所以这里是黑客。

在 "landscape" VC 的 viewDidLoad/viewDidAppear 中,推送另一个通用 VC 对象并随后弹出它

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

这曾经在 iOS7 中有效,之后我们切换到模态。所以这现在可能适用于更高版本。