我的应用程序支持 iPhone 并使用 iPad 进行扩展(不支持 iPad 上的全屏)。
只要此功能在 iphone(所有 ios 版本)上正常工作,我们就会以编程方式将方向更改为横向。但屏幕旋转功能不适用于 iPad Pro iPadOS 15.0 及更高版本。我调试它,旋转状态设置为“方向”键,但 UIViewController.attemptRotationToDeviceOrientation() 函数似乎没有将状态反映给应用程序。
下面是执行屏幕旋转的代码。你能帮忙检查一下吗?
环境:
Xcode: 12.2(12B45b), Version 13.0 (13A233)
Simulator: iPad Pro(12.9-inch) OS15.0
Device: iPad Pro 11inch (gen2) OS 15.0.2
应用委托:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return ScreenOrientationManager.shared.currentOrientation
}
旋转管理器类:
class ScreenOrientationManager: NSObject {
static let shared: ScreenOrientationManager = ScreenOrientationManager()
var currentOrientation: UIInterfaceOrientationMask = .portrait
@discardableResult
func rotateScreen() {
if self.currentOrientation == .portrait {
self.currentOrientation = .landscape
UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
} else {
self.currentOrientation = .portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}
UIViewController.attemptRotationToDeviceOrientation() // *→ Not working on iPadOS15*
}
项目设置
<key>UIRequiresFullScreen</key>
<false/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>