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
}
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.