在 Swift iOS 中设置设备方向 [英] Setting device orientation in Swift iOS
问题描述
我正在开发适用于 iPhone 的 swift 应用程序.我的应用程序中有一个模态视图,我只想在纵向视图中显示.
I am working on a swift app for iPhone. There is a modal view in my application that I want only to be in portrait view.
我的问题是,如何以编程方式强制手机不允许旋转?换句话说,我正在寻找不允许以横向模式显示模式视图的代码(打开纵向旋转锁定).
My question is, how do I programmatically force the phone to not allow rotation? In other words, I am looking for code that will not allow a modal view to be displayed in landscape mode (turning on portrait rotation lock).
这仅适用于 1 个模态视图,因此我无法关闭整个应用程序的旋转,否则我将完全禁用旋转.
This is just for 1 modal view, so I can't turn off rotation for the entire app, otherwise I would just disable rotation altogether.
我在我的研究中找到了代码这里但它在目标 C 中,以防万一.谢谢!
I found code in my research here But it is in objective C, in case that helps. Thanks!
推荐答案
可以在每个需要竖屏的view的ViewController中粘贴这些方法:
You can paste these methods in the ViewController of each view that needs to be portrait:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
这篇关于在 Swift iOS 中设置设备方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!