简体   繁体   中英

Reset autorotation in Xcode after force landscape orientation in one view controller (swift 4)

An app in which I've begun to work supports all device orientations, except on of the views, which is landscape.

To force this orientation, I set the lanscape orientation value via AppDelegate:

AppDelegate.swift

var shouldSupportAllOrientation = true
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if (shouldSupportAllOrientation == true){
       return UIInterfaceOrientationMask.all
    }
    return UIInterfaceOrientationMask.landscape
}

ViewController.swift

let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = false
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

But when the app navigates to other view controller, and the device is in portrait orientation (physically), views appear on landscape mode until device is rotated.

Is there a way to detect the 'real device orientation'?

Thank you

I faced the similar issue. After forcing the UIDevice.current.orientation , it seems that device can't update it anymore (which looks expected since it's a get-only property and represents physical orientation)

Try override supportedInterfaceOrientations in ViewController.swift:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscape
}

After that if you open this VC, the interfaceOrientation is landscape, but UIDevice.current.orientation is the real one. Just a note: you might want to flip width and height of your view if you work with frames and bounds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM