我用了下面的方法让它可行,但是没有用
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/