I always received this error when I tried to allowed only portrait orientation on my controller: Error Domain=UISceneErrorDomain Code=101 "None of the requested orientations are supported by the view controller. Requested: landscapeLeft; Supported: portrait" UserInfo={NSLocalizedDescription=None of the requested orientations are supported by the view controller. Requested: landscapeLeft; Supported: portrait}
I called this method:
func updateOrientation(orientation: UIInterfaceOrientationMask) {
if #available(iOS 16, *) {
DispatchQueue.main.async {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
self.setNeedsUpdateOfSupportedInterfaceOrientations()
self.navigationController?.setNeedsUpdateOfSupportedInterfaceOrientations()
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation)) { error in
print(error)
print(windowScene?.effectiveGeometry )
}
}
}
}
Did someone face the same issue ?
J.Y.527 found a solution : https://stackoverflow.com/a/73735976/2858994
It looks like a workaround, but it's fixed the problem in my case.
the trick is to update the AppDelegate supported orientation when you need to rotate.
In Your AppDelegate :
var orientation: UIInterfaceOrientationMask = .all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return orientation
}
Then in your controller :
static func SwitchOrientation(orientation : UIInterfaceOrientationMask, viewController : UIViewController){
if #available(iOS 16.0, *) {
(UIApplication.shared.delegate as? AppDelegate)?.orientation = orientation
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation))
UIApplication.navigationTopViewController()?.setNeedsUpdateOfSupportedInterfaceOrientations()
DispatchQueue.main.async {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
viewController.setNeedsUpdateOfSupportedInterfaceOrientations()
viewController.navigationController?.setNeedsUpdateOfSupportedInterfaceOrientations()
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation)) { error in
print(error)
print(windowScene?.effectiveGeometry ?? "")
}
}
} else{
UIDevice.current.setValue(orientation, forKey: "orientation")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With