1

我的应用程序支持 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>
4

2 回答 2

0

你很好,这是一个很好的问题,代表很多人感谢你。

我可以针对您的案例具体解释如下: 问题是:iPadOS 和 iOS 之间的差异之一在于 Apple 开发的 iPadOS 系统的核心支持 Slide Over 和 Split View 等功能,这些功能可以使用多个不同的应用程序同时,这意味着管理窗口的公认方式在 iPadOS 屏幕上使用具有相同主要活动的多个窗口。

UIViewController.attemptRotationToDeviceOrientation() 仍然有效,但您弄错了,它适用于具有最高分配层的窗口(相反,不支持 iOS 以确保用户设计),此窗口不是您的应用程序主屏幕的容器窗口。

要解决您的问题,您需要检查您的项目是否正在使用另一个具有更高杠杆的窗口(检查:window.windowLevel)?您需要删除或设置属性以隐藏较高楼层的窗户。例如:window.isHidden = true。

干杯。

于 2021-10-16T22:43:21.750 回答
0

您可以将 viewcontroller 的presentationStyle 设置为全屏来修复它..!!

于 2021-10-14T10:26:04.083 回答