使用Swift将方向设置为纵向 [英] Set Orientation to Portrait with Swift
问题描述
我有一个应用程序,其中一些屏幕应该处于纵向模式,其中一些处于横向模式。例如,主菜单是纵向,并且有一个相机按钮。相机应仅在横向上可用。我正在使用Storyboard和NavigationController。我应该在哪里以及如何做到这一点?
I have an app where some of the screens should be in portrait mode and some of them in landscape. For example, main menu is portrait and there is a camera button. The camera should be available only in landscape. I am using Storyboard and NavigationController. Where and what should I do to achieve this?
我已经尝试包括
shouldAutorotate() -> Bool {return false or return true}
两者均无效。我设置了
supportedInterfaceOrientation
我拥有的每个viewcontroller。什么都没有帮助。我需要一个功能,我可以调用强制屏幕进入横向/纵向模式。
for every viewcontroller I have. Nothing helped. I need a function what I can call to force the screen into landscape/portrait mode.
推荐答案
在ViewDidLoad中(或您想要的任何地方)使用此行代码。
Use this line of code in your ViewDidLoad (or wherever you want).
Swift 1
UIDevice.currentDevice()。setValue(UIInterfaceOrientation.LandscapeLeft.rawValue,forKey: 定向)
3分钟前在Swift 1.2中测试Xcode 6.3
Tested 3 minutes ago in Swift 1.2 Xcode 6.3
Swift 3 (感谢Fa.Shapouri)
Swift 3 (Thanks to Fa.Shapouri)
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
这篇关于使用Swift将方向设置为纵向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!