如何为特定的 ViewControllers 启用或禁用横向模式?

问题描述

我在 Xcode 中启用了横向左右模式。

如何为特定的 ViewController 启用或禁用横向模式?

对于某些 VC,我需要横向模式,而对于某些 VC,我只需要纵向模式。

如何在 Objective C 中以编程方式实现它?

enter image description here

解决方法

在您的视图控制器中,覆盖 supportedInterfaceOrientations 属性

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
}

您只需要确保不要返回与“设备方向”设置冲突的任何内容,否则应用程序将崩溃。