iOS 16 中报告错误的界面方向

Giz*_*odo 5 xcode rotation uiinterfaceorientation ios swift

我一直在使用以下方法来检测 iOS 14/15 中当前的界面方向:

UIApplication.shared.windows.first!.windowScene!.interfaceOrientation 
Run Code Online (Sandbox Code Playgroud)

现在,在 iOS 16 Beta(1 和 2)中,它的报告不正确。风景被报道为肖像颠倒。

Kri*_*oth 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)
    }
Run Code Online (Sandbox Code Playgroud)

Apple 在 iOS_16 中弃用了与 UIDevice 方向相关的 api,而是使用requestGeometryUpdate方法来请求更新窗口场景几何形状。

有关详细信息,请查看以下苹果开发人员文档。 请求几何更新

如果您仍然无法成功旋转设备,请检查以下链接以了解可能的根本原因和解决方案。

UISceneErrorDomain Code=101 问题及解决方案