var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
struct OrientationManager {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, rotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
}
attemptRotationToDeviceOrientation
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
OrientationManager.lockOrientation(.landscape, andRotateTo: .landscapeRight)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
OrientationManager.lockOrientation(.all)
}
if UIDevice.current.orientation == .portraitUpsideDown || UIDevice.current.orientation == .portrait {
AppUtility.lockOrientation(.portrait)
} else {
AppUtility.lockOrientation(.landscape)
}