ios - 如何禁用 UIApplication.keyWindow() 呈现的 ViewController 上的方向

标签 ios swift uiviewcontroller rotation

我有一个可以在应用程序中的任何位置呈现的 ViewController。我用这段代码 UIApplication.sharedApplication().keyWindow?.rootViewController?.presentedViewController?.presentViewController 来呈现它。所以我想禁用方向,只支持纵向。我尝试了很多解决方案,实现了shouldAutorotate/supportedInterfaceOrientations/preferredInterfaceOrientationForPresentation,但没有任何帮助。有人遇到过这个问题吗?

最佳答案

你在viewDidLoad中尝试过这个吗?

viewDidLoad() {
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
    shouldAutorotate()
}

关于ios - 如何禁用 UIApplication.keyWindow() 呈现的 ViewController 上的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35847115/

相关文章:

iphone - 将工具栏添加到 UITableViewController

ios - 无法在模拟器中运行应用程序

ios - 在 SwiftUI 中创建 ViewModifier 和 View 扩展的区别

ios - 从 NSDateComponents 创建 NSDate 关闭一天

swift - 如何从 Realm 结果创建数组

ios - 应用重新安装后 Firebase 消息传递不起作用

ios - 如何使用委托(delegate)从一个 Controller 向另一个 Controller 发送消息?

ios - 将 objective-c 和 swift 库与 Cocoapods 一起使用时生成错误

ios - 使用 Storyboard中的 View 将 XIB 中 UIView 的 UITextField 加载到 ViewCrontroller

iphone - View 交换技术