4

I have been using the following to detect the current interface orientation in iOS 14/15:

UIApplication.shared.windows.first!.windowScene!.interfaceOrientation 

Now in iOS 16 Beta (1 and 2), it is reporting incorrectly. Landscape is being reported as Portrait Upsidedown.

Gizmodo
  • 3,151
  • 7
  • 45
  • 92
  • What is the question? If there's a bug in the beta, you should report it via Feedback Assistant – johnny Jun 23 '22 at 20:18
  • I am trying to see if there is a way around it. – Gizmodo Jun 24 '22 at 01:22
  • Probably the only way around it is to downgrade from the beta or wait for the next beta version to drop. In the meantime, this kind of thing is better posted on the apple developer forums where actual Apple engineers can respond directly. – johnny Jun 28 '22 at 18:14

2 Answers2

4
 if #available(iOS 16, *) {
        DispatchQueue.main.async {
            let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
            self.setNeedsUpdateOfSupportedInterfaceOrientations()
            self.navigationController?.setNeedsUpdateOfSupportedInterfaceOrientations()
            windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscape)) { error in
                print(error)
                print(windowScene?.effectiveGeometry ?? "")
            }
        }
        
    } else {
        appDelegate.myOrientation = .landscape
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
        UIView.setAnimationsEnabled(true)
    }

Apple deprecated UIDevice orientation related api's in iOS_16 instead use requestGeometryUpdate method which requests an update to the window scenes geometry.

For detailed information kindly check the following apple developer documentation. requestGeometryUpdate

If you still not able to rotate the device successfully, kindly check the below link for possible root cause and the solution.

UISceneErrorDomain Code=101 issue and solution

Krishnarjun Banoth
  • 1,410
  • 1
  • 15
  • 30
0

I managed to deal with it in this way:

if (@available(iOS 16, *)) {
    float windowWidth = gameScene.view.window.frame.size.width;
    float windowHeight = gameScene.view.window.frame.size.height;
    
    if (windowWidth > windowHeight) {
        orientation = UIInterfaceOrientationLandscapeLeft;
    } else {
        orientation = UIInterfaceOrientationPortrait;
    }
} else {
    orientation = gameScene.view.window.windowScene.interfaceOrientation;
}
HangarRash
  • 7,314
  • 5
  • 5
  • 32