使用Swift设置纵向方向(竖屏)

4
我有一个应用程序,其中一些屏幕应该是竖屏模式,而另一些屏幕则应该是横屏模式。例如,主菜单是竖屏,有一个相机按钮。相机只能在横屏模式下使用。我正在使用Storyboard和NavigationController。我应该在哪里做什么来实现这一点?
我已经尝试过包含

shouldAutorotate() -> Bool {return false or return true} 

无论哪种方法都没有生效。我已经设置了。
supportedInterfaceOrientation 

每个视图控制器我都试过了,但都无济于事。我需要一个函数,可以调用它来强制屏幕进入横/竖屏模式。

这里已经回答了 答案 - MacKentoch
这两个解决方案都对我没用。:( - MegaX
1个回答

15

在您的ViewDidLoad中(或任何您想要的地方)使用以下代码行。

Swift 1 UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeLeft.rawValue, forKey: "orientation")

在Swift 1.2 Xcode 6.3中测试,3分钟前。

Swift 3(感谢Fa.Shapouri)

UIDevice.current.setValue(UIInterfaceOrientation.landscapeLe‌​ft.rawValue, forKey: "orientation")

2
苹果会拒绝使用它的应用程序吗? - He Yifei 何一非
Swift 3: UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation") Swift 3:UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation") - Fa.Shapouri
@YifeiHe何一非 不,这个解决方案对于应用商店来说是可以的。 - aaannjjaa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接