原文地址:https://stackoverflow.com/questions/40413567/overriding-shouldautorotate-not-working-in-swift-3
var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
使用:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//let value = UIInterfaceOrientation.landscapeLeft.rawValue
//UIDevice.current.setValue(value, forKey: "orientation")
AppDelegate.AppUtility.lockOrientation(.landscapeLeft)
}
网友评论