当我尝试在控制器上仅允许纵向方向时,我总是收到此错误:Error Domain=UISceneErrorDomain Code=101“视图控制器不支持所请求的方向。请求:landscapeLeft;支持:纵向”UserInfo={NSLocalizedDescription=视图控制器不支持所请求的方向。请求:景观左;支持:纵向}
我称这个方法为:
func updateOrientation(orientation: UIInterfaceOrientationMask) {
if #available(iOS 16, *) {
DispatchQueue.main.async {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
self.setNeedsUpdateOfSupportedInterfaceOrientations()
self.navigationController?.setNeedsUpdateOfSupportedInterfaceOrientations()
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation)) { error in
print(error)
print(windowScene?.effectiveGeometry )
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人面临同样的问题吗?
在 iOS 16 之前,横向呈现单屏对于纵向应用来说是可以的。工作代码如下。
备注:整个应用程序仅处于纵向模式。
override public var shouldAutorotate: Bool {
return false
}
override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override public var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeLeft
}
Run Code Online (Sandbox Code Playgroud)
我找到了解决方案,但它适用于UIWindowScene,但我需要 UIWindow 中的解决方案。我需要帮助来修复 iOS 16 中的问题。
Xcode - 14.0,iOS - 16.0,模拟器 - 14 Pro
如果有人需要,我可以准备演示。