iOS 14 UIDevice.current.setValue(xxx, forKey: "orientation") not work

Viewed 380

iOS 14 set device orientation with UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation") not work, my app do not support multiple scenes. This not work maybe for multiple scenes, what's the alternative method?

sample code as below:

@IBAction func rotate(_ sender: Any) {
    guard let scene = UIApplication.shared.windows.first?.windowScene else {
        // this do not invoked
        return
    }
    let orientation = scene.interfaceOrientation
    if orientation.uintValue == UIDeviceOrientation.portrait.rawValue {
        let number = NSNumber.init(value: UIDeviceOrientation.landscapeLeft.rawValue)
        UIDevice.current.setValue(number, forKey: "orientation")
    }else {
        let number = NSNumber.init(value: UIDeviceOrientation.portrait.rawValue)
        UIDevice.current.setValue(number, forKey: "orientation")
    }

}

Xcode Version 12.1 (12A7403), iPhone is iOS 14.0.1.

0 Answers
Related