ios - 覆盖 shouldAutorotate 在 Swift 3 中不起作用

标签 ios swift autorotate

我试图阻止一个 UIViewController 上的旋转,但我无法实现。

我正在做这样的事情:

open override var shouldAutorotate: Bool {
    get {
        return false
    }
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get {
        return .portrait
    }
}

UIViewControler 仍然在旋转。 UIViewController 位于模态打开的 UINavigationController 中。

我从这里看了很多问题,但没有一个答案适合我。

在 Swift 2 中,我曾经重写 shouldAutorotate,但在 Swift 3 中,该函数不再存在。

我怎样才能在 Swift 3 中完成我以前在 Swift 2 中所做的事情?

最佳答案

如果我可以多次重现这种行为,我不知道为什么要投票结束这个问题。 UIViewController 位于模态打开的 UINavigationController 中。

这就是我为解决问题所做的。

我创建了这个类并设置了 UINavigationController 包含了我想防止旋转的 UIViewController

class NavigationController: UINavigationController { 

    override var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

}

就是这样,它对我有用

关于ios - 覆盖 shouldAutorotate 在 Swift 3 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40413567/

相关文章:

swift - 在 Swift 4 中递归解析数组

xcode - iOS 5屏幕初始自动旋转

ios - iOS 13 中的自动旋转错误

ios - 我可以为图像设置动画吗 iOS8 LaunchScreen.xib

ios - 核心数据 sqlite-wal 文件在插入 ~5000 行时变得巨大(>7GB)

ios - Swift 通过 UITabBarController 访问 viewController 的函数

ios - 根据照片方向设置自动旋转和首选方向

ios - 将 UICollectionView 中的所有图片保存为一张图片

swift - 是否可以将节点保持在适当的位置

Swift、Cocoapods 和 ios 7 兼容性、OAStackView