You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 16 控制器仅允许竖屏时出现Scene方向错误求助

解决iOS16设置竖屏时UISceneErrorDomain Code=101的问题

这个错误的核心原因是你请求的界面方向和当前控制器(或导航控制器)实际支持的方向不匹配——错误日志里明确显示请求了landscapeLeft,但控制器只支持portrait,可以从以下几个点逐一排查解决:

  • 确认调用参数是否正确
    检查你调用updateOrientation(orientation:)时传入的参数是不是.portrait,如果误传了.landscapeLeft或其他横屏方向,必然触发这个错误。

  • 让当前控制器明确声明支持竖屏
    在你的控制器类中重写supportedInterfaceOrientations属性,强制指定仅支持竖屏:

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
  • 处理导航控制器的方向覆盖问题
    如果你的控制器嵌套在UINavigationController里,导航控制器的supportedInterfaceOrientations会默认覆盖子控制器的设置。给导航控制器添加扩展,让它继承当前栈顶控制器的方向规则:

    extension UINavigationController {
        override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return topViewController?.supportedInterfaceOrientations ?? .portrait
        }
    }
    
  • 优化iOS16的方向请求逻辑
    调整updateOrientation方法的代码顺序,确保先更新控制器的支持方向,再请求场景更新,同时避免冗余操作:

    func updateOrientation(orientation: UIInterfaceOrientationMask) {
        if #available(iOS 16, *) {
            DispatchQueue.main.async {
                self.setNeedsUpdateOfSupportedInterfaceOrientations()
                guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return }
                windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: orientation)) { error in
                    if let error = error {
                        print("方向更新错误:\(error.localizedDescription)")
                    }
                }
            }
        }
    }
    

最后补充检查:项目的Info.plist中UISupportedInterfaceOrientations(iPad对应UISupportedInterfaceOrientations~ipad)必须包含portrait方向,全局支持的方向范围要覆盖控制器的限制方向,否则也会出现异常。

内容的提问来源于stack exchange,提问作者Hatoine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 05:15:40