[ACCEPTED]-is Force View Controller Orientation working in iOS 16 beta-ios16

Accepted answer
Score: 10

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

if #available(iOS 16.0, *) {

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

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

} 
Score: 2

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

[UIViewController setNeedsUpdateOfSupportedInterface

You 1 may give it a try.

Score: 2

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 1 Helper,

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

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

Score: 0

Apple released new API which is replaced 2 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 1 I am having problem about UIDevice.orientationDidChangeNotification , it is not working

Score: 0

It works for me:

    import Foundation
    import UIKit

    extension UIViewController {
    
    func setDeviceOrientation(orientation: UIInterfaceOrientationMask) {
        if #available(iOS 16.0, *) {
            let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
            windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation))
        } else {
            UIDevice.current.setValue(orientation.toUIInterfaceOrientation.rawValue, forKey: "orientation")
        }
    }
}

extension UIInterfaceOrientationMask {
    var toUIInterfaceOrientation: UIInterfaceOrientation {
        switch self {
        case .portrait:
            return UIInterfaceOrientation.portrait
        case .portraitUpsideDown:
            return UIInterfaceOrientation.portraitUpsideDown
        case .landscapeRight:
            return UIInterfaceOrientation.landscapeRight
        case .landscapeLeft:
            return UIInterfaceOrientation.landscapeLeft
        default:
            return UIInterfaceOrientation.unknown
        }
    }
}

How to use it?

Just call it on your UIViewController:

setDeviceOrientation(orientation: .landscapeRight)

0

More Related questions