is Force View Controller Orientation working in iOS 16 beta

Viewed 2470

According to the iOS & iPadOS 16 Beta 3 Release Notes:- Attempting to set an orientation on UIDevice via setValue:forKey: isn’t supported and no longer works. Instead, they say use: preferredInterfaceOrientationForPresentation.

In my case, force view controller orientation is not working in iOS 16 beta either by using preferredInterfaceOrientationForPresentation or requestGeometryUpdate.

Previously, UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation") was working fine.

5 Answers

My problem with my code below is that I'm trying to do it when closing a modal view and the view under it are not updated quick enough. If I put the requestGeometryUpdate on a separate button then when I close the view it work.

if #available(iOS 16.0, *) {

    let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene

    windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait))

} 

I noticed my issue seems like resolved by calling method below:

[UIViewController setNeedsUpdateOfSupportedInterface

You may give it a try.

setValue:forKey is a method of old NSObject (NSKeyValueCoding). It's not official documented and supported by UIDevice class. Using it is considering using a private api. Apple can terminate it anytime they want.

Apple released new API which is replaced with setValue:forKey:"orientation". Apple update

guard let windowScene = view.window?.windowScene else { return }
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .landscape)) { error in
// Handle denial of request.
}

But I am having problem about UIDevice.orientationDidChangeNotification , it is not working

It works for me.

In AppDelegate,

var orientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return orientation
    }

In view controller,

(UIApplication.shared.delegate as? AppDelegate)?.orientation = .landscapeRight
                    
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight))

UIApplication.navigationTopViewController()?.setNeedsUpdateOfSupportedInterfaceOrientations()

In Helper,

extension UIApplication {
    class func navigationTopViewController() -> UIViewController? {
            let nav = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController
            return  nav?.topViewController
        }
}
Related