我需要在单视图控制器中提供方向支持
我尝试但没有用。现在在整个应用中定位。
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
}
答案 0 :(得分:0)
将以下代码放入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
}
答案 1 :(得分:0)
问题已通过bmjohns答案解决 请访问并检查。
@bmjohns答案更新版本
// set orientations you want to be allowed in this property by default
var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
self.lockOrientation()
return true
}
func unlockOriention () {
self.orientationLock = .all
}
func lockOrientation() {
self.orientationLock = .portrait
}
SecondViewController.swift
支持方向
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.unlockOriention()
}