ios - 如何旋转到只有一个 View Controller 的横向

标签 ios swift

我用了下面的方法让它可行,但是没有用

override func viewDidLoad() {
    super.viewDidLoad()
    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeLeft
}

override var shouldAutorotate: Bool {
    return true
}    

最佳答案

如果你想强制特定的 viewController 只旋转到横向然后在 AppDelega 文件中添加以下代码

public var orientationLock = UIInterfaceOrientationMask.all

    static var shared : AppDelegate {
        return UIApplication.shared.delegate as! AppDelegate
    }


    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return self.orientationLock
    }

    func lockDeviceOrientation(orientationLock: UIInterfaceOrientationMask, rotateOrientation:UIInterfaceOrientation) {
        AppDelegate.shared.orientationLock = orientationLock
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }

并在特定的 viewController 中添加以下代码,您希望强制仅在横屏中旋转

 override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        AppDelegate.shared.orientationLock = .landscapeLeft
    }

关于ios - 如何旋转到只有一个 View Controller 的横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56110334/

相关文章:

ios - SKScene识别GameScene的高分

ios - 使用按钮操作发送单元格字符串值

ios - 如何将表格 View 单元格计数值分配给编程 UILabel?

ios - 如何检索 MTKView/UIView/NSView 使用的当前纹理

ios - 为什么 SimpleAuth CocoaPod 不能与 Swift Bridging Header 文件一起使用?

ios - 在 viewController 中实例化时如何设置 UIImageView 的大小/框架和 contentMode?

ios - 测试期间的 Xcode 8、Crashlytics 和推送通知

iOS - 圆形渐变

ios - 循环以获取自定义 NSObject 类 swift 中的数据

ios - 将 Swift._SwiftDeferredNSArray' (0x1096d26b8) 获取到核心数据中的 'NSMutableArray