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

如何阻止ARSCNView随设备摆放方向自动旋转?

解决方案
  • 第一步:检查并关闭iPad多窗口支持(若不需要多窗口功能)
    你之前的全局方向设置不生效,绝大多数情况是iPadOS的多窗口特性导致的:iPadOS中开启「支持多窗口」的App会忽略全局方向限制,需要每个窗口单独处理方向。
    操作路径:打开项目Info.plist,找到Application Scene Manifest -> Enable Multiple Windows,将值改为NO;或者直接在项目General设置的「Deployment Info」区域取消勾选「Supports multiple windows」选项。
  • 第二步:修复容器控制器的旋转逻辑
    如果你的ViewController是嵌套在UINavigationController、UITabBarController等容器控制器内,仅在子ViewController中重写旋转属性是无效的,需要给容器控制器添加旋转逻辑控制。
    示例代码(子类化UINavigationController):
    class CustomNavigationController: UINavigationController {
        override var shouldAutorotate: Bool {
            // 取当前显示的顶部ViewController的旋转配置
            return topViewController?.shouldAutorotate ?? false
        }
        
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return topViewController?.supportedInterfaceOrientations ?? .portrait
        }
    }
    
    之后把你的导航控制器替换为这个自定义子类即可。
  • 第三步:确认ViewController的旋转配置正确
    你原来在ViewController中写的代码保留即可:
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    override var shouldAutorotate: Bool {
        return false
    }
    
  • 可选兜底方案:强制固定ARSCNView的方向
    如果做完上述操作后AR视图仍然异常旋转,可以手动禁用ARKit的自动视图调整,强制固定变换:
    // 在viewDidLoad中添加
    if let configuration = sceneView.session.configuration as? ARWorldTrackingConfiguration {
        // 固定世界对齐方式为重力对齐,避免方向跟随设备变动
        configuration.worldAlignment = .gravity
        sceneView.session.run(configuration)
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 19:54:03