iPad Pro iPadOS15.0及以上版本强制屏幕旋转功能失效问题排查
iPadOS 15及以上版本调整了iPhone应用兼容缩放运行场景的屏幕旋转逻辑,原有直接修改orientation私有键值+调用全局attemptRotationToDeviceOrientation()的方式会被系统忽略,必须主动触发视图控制器的旋转配置刷新才能生效。
解决步骤
- 调整info.plist配置
将UIRequiresFullScreen字段的值改为true,iPadOS 15+对非全屏应用的强制旋转权限做了限制,即使是iPhone兼容模式也需要配置该参数才能触发主动旋转。
修改后的配置片段:
<key>UIRequiresFullScreen</key> <true/>
其他UISupportedInterfaceOrientations相关配置保持不变即可。
- 调整ScreenOrientationManager的rotateScreen实现,补充iOS15+适配逻辑
修改后的完整代码如下:
class ScreenOrientationManager: NSObject { static let shared: ScreenOrientationManager = ScreenOrientationManager() // 此处修改为明确的单方向枚举,避免方向mask不匹配 var currentOrientation: UIInterfaceOrientationMask = .portrait @discardableResult func rotateScreen() { // 开启设备方向通知,确保系统能接收到方向变更事件 UIDevice.current.beginGeneratingDeviceOrientationNotifications() defer { UIDevice.current.endGeneratingDeviceOrientationNotifications() } let targetOrientation: UIInterfaceOrientation if self.currentOrientation == .portrait { currentOrientation = .landscapeRight targetOrientation = .landscapeRight } else { currentOrientation = .portrait targetOrientation = .portrait } // 写入方向值 UIDevice.current.setValue(targetOrientation.rawValue, forKey: "orientation") // iOS15+适配:主动触发当前顶层视图控制器的旋转配置刷新 if #available(iOS 15.0, *) { // 获取当前最上层显示的视图控制器 guard let window = UIApplication.shared.windows.first, let rootVC = window.rootViewController else { return } var topVC = rootVC while let presentedVC = topVC.presentedViewController { topVC = presentedVC } topVC.setNeedsUpdateOfSupportedInterfaceOrientations() } // 触发全局旋转 UIViewController.attemptRotationToDeviceOrientation() } }
AppDelegate中的application(_:supportedInterfaceOrientationsFor:)实现无需修改。
内容的提问来源于stack exchange,提问作者LAI THE ANH
相关产品推荐
相关产品推荐

