我需要在单 View Controller 中提供方向支持
我尝试过,但没有成功。现在整个应用程序的方向。
override func viewDidLoad() {
super.viewDidLoad()
self.lockOrientation()
}
func lockOrientation() {
let orientationValue = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(orientationValue, forKey: "orientation")
}
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
最佳答案
将以下代码放入AppDelegate
仅在项目常规设置中将设备方向保留为纵向
var restrictRotation: Bool = true
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if restrictRotation {
return .portrait
} else {
return .all
}
}
在下面输入您想要允许/禁用方向的代码。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.restrictRotation(false) //TRUE TO RESTRICT ORIENTATION && FALSE TO ALLOW ORIENTATION
}
func restrictRotation(_ restriction: Bool) {
let appDelegate = UIApplication.shared.delegate as? AppDelegate
appDelegate?.restrictRotation = restriction
}
关于ios - 单 View Controller swift 5 中的方向支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56421288/