Force rotation not working properly

We rotate the portrait to landscape then we force rotate to portrait app rotates but the screen seems to be divided into two parts

My sample code

class OrientationHelper {
    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)

        DispatchQueue.main.async {
            UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
            UIViewController.attemptRotationToDeviceOrientation()
        }
    }
}
// Appdelegte

var orientationLock = UIInterfaceOrientationMask.portrait

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return orientationLock
    }

Answered by Frameworks Engineer in 828152022

This line of code:

UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")

Is effectively using private API / relying on undocumented implementation details. While setValue(:forKey:) is indeed public, unless you wrote the class that you are calling it on, or using documented keys when calling it, you are likely relying on private API or undocumented implementation details.

If you want to update the orientation of your application there is UIViewController.setNeedsUpdateOfSupportedInterfaceOrientations() alongside the existing UIViewController orientation API.

Accepted Answer

This line of code:

UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")

Is effectively using private API / relying on undocumented implementation details. While setValue(:forKey:) is indeed public, unless you wrote the class that you are calling it on, or using documented keys when calling it, you are likely relying on private API or undocumented implementation details.

If you want to update the orientation of your application there is UIViewController.setNeedsUpdateOfSupportedInterfaceOrientations() alongside the existing UIViewController orientation API.

Dear Team,

Thanks for quick reply. We tried UIViewController.setNeedsUpdateOfSupportedInterfaceOrientations() but not yet rotate the viewcontroller. we used below lines for force rotate while tap button action

  var landscape: UIInterfaceOrientationMask = .portrait
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return landscape
    }
    func forceRotateToLandscape() {
        DispatchQueue.main.async {
            if #available(iOS 16.0, *) {
                self.landscape = .landscape
                self.setNeedsUpdateOfSupportedInterfaceOrientations()

            } else {
                // Fallback on earlier versions
            }
            }
        }
        

        func forceRotateToPortrait() {
            if #available(iOS 16.0, *) {
                self.landscape = .portrait
                setNeedsUpdateOfSupportedInterfaceOrientations()
            } else {
                // Fallback on earlier versions
            }

        }

we supported portrait method for my entire app but we forcefully rotate landscape/portrait particular view controller only. Kindly do the needful

Force rotation not working properly
 
 
Q