[Fix] iOS16+ Present UIViewController in landscape only for single screen not working

The problem “iOS16+ Present UIViewController in landscape only for single screen not working” is seen when you are working on swift version 5.7 and are trying to present a single screen in the landscape using portrait applications.

The problem, “iOS16+ Present UIViewController in landscape only for single screen not working” is seen because in the iOS 16 release notes you can note that some methods have been deprecated and is no longer used in the latest version of iOS 16.

The list of deprecations which is no longer used in the latest version of iOS 16 is given below and the source for it is the apple official documentation, which can be found here.

Below is the list of all deprecations in iOS 16 release:

[UIViewController shouldAutorotate] has been deprecated is no longer supported.

[UIViewController attemptRotationToDeviceOrientation] has been deprecated and replaced with [UIViewController setNeedsUpdateOfSupportedInterfaceOrientations]

As per official OS 16 Release Notes, [UIViewController shouldAutorotate] and as as workaround use the replaced [UIViewController setNeedsUpdateOfSupportedInterfaceOrientations] to fix the error.

[UIViewController shouldAutorotate] has been deprecated is no longer supported. [UIViewController attemptRotationToDeviceOrientation] has been deprecated and replaced with [UIViewController setNeedsUpdateOfSupportedInterfaceOrientations].

Workaround: Apps relying on shouldAutorotate should reflect their preferences using the view controllers supportedInterfaceOrientations. If the supported orientations change, use `-[UIViewController setNeedsUpdateOfSupportedInterface

UIKit New Features
iOS apps can now request rotation using [UIWindowScene requestGeometryUpdate:errorHandler:] and providing a UIWindowSceneGeometryPreferencesIOS object containing the desired orientations. (95229615)

The deprecations is the reason why the autorotate feature is not working, and is the reason why you are receiving the problem.

To fix the error “iOS16+ Present UIViewController in landscape only for single screen not working”, applications that rely on ‘shouldAutorotate’ should use the ‘supportedInterfaceOrientations’ of the view controller to reflect their preferences. Use the command “-[UIViewController setNeedsUpdateOfSupportedInterface” to fix the error.

Steps to Fix the error “iOS16+ Present UIViewController in landscape only for single screen not working”:

To fix the problem you should know that applications that rely on ‘shouldAutorotate’ should use the ‘supportedInterfaceOrientations’ of the view controller to reflect their preferences.

You will have to use the command “-[UIViewController setNeedsUpdateOfSupportedInterface” if the supported orientations change in your system.

[UIViewController setNeedsUpdateOfSupportedInterface

Alternate method

There also an alternate method available which will help you to avoid the problem.

To do so follow the steps mentioned below:

To manage the orientation, maintain this function in AppDelegate, the function is given below:

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

Add the lines given below to the variable defining section of the ViewController you wish to force the orientation in:

var forceLandscape: Bool = false

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
           forceLandscape ? .landscape : .portrait
}

Next there will be a change in the forceLandscape, which will subsequently be followed by a change in the supportedInterfaceOrientations.

Next up we will be setting up the force Landscape to update trigger. Add the code mentioned below to set up your update:

if #available(iOS 16.0, *) {
            self.forceLandscape = true
            guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return }
            self.setNeedsUpdateOfSupportedInterfaceOrientations()
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
                windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight)){
                        error in
                        print(error)
                        print(windowScene.effectiveGeometry)
                }
            })

The forceLandscape will be updated as a result, and it will verify the orientation and update as necessary.

This should help you fix and understand the problem, “Code signing is required for product type ‘Application’ in SDK ‘iOS 10.0′”.