【问题标题】:iPad Pro iPadOS 15.0.2 Orientation Changes Not workingiPad Pro iPadOS 15.0.2 方向更改不起作用
【发布时间】:2021-12-02 16:48:01
【问题描述】:

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

RotationManager 类:

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>

【问题讨论】:

    标签: ios swift ipad ipados15


    【解决方案1】:

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

    【讨论】:

    • 还有其他方法吗?因为我在一次视图上使用旋转(使用自动布局约束)。调用的旋转函数在 iphone 上工作正常,但在 ipad 上不工作。有点奇怪。感谢您的评论。
    【解决方案2】:

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

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

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

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

    干杯。

    【讨论】:

    • 我尝试创建一个新项目,使用与上面相同的测试代码。我确定只有 1 个窗口正在使用中。使用该项目屏幕旋转在 iphone(所有版本)上运行良好,但不适用于 iPadOS 15.0 及更高版本。如果你有其他方法并与我分享,我会很高兴。感谢您的评论。
    • 你能把你的例子上传到 git 上吗?我可以帮助你,你可以,而且你可以继续帮助许多其他人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多