为什么Xcode 13完全忽略设备方向设置?有什么解决方案?
Xcode 13 设备方向设置不生效解决方案
配置校验及修复步骤
- 检查根容器控制器的方向重写逻辑:UINavigationController、UITabBarController等全局根容器的方向配置优先级远高于项目全局设置,如果项目中重写了这类容器的方向代理方法,需要修改对应逻辑强制返回竖屏:
// Swift 示例,重写导航控制器的方向属性 override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait }// Objective-C 示例,重写导航控制器的方向方法 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - 校验当前运行target的info.plist配置:Xcode 13存在多target时易出现配置错配问题,确认info.plist中
Supported interface orientations数组下仅保留Portrait (bottom home button)一项,删除其余横屏、竖屏顶部朝上的相关条目。 - 清理Xcode缓存后重新构建:配置未生效大概率是缓存导致,按顺序执行以下操作后重新运行:
- 按快捷键
Command + Shift + K清理现有构建产物 - 关闭Xcode后进入
~/Library/Developer/Xcode/DerivedData目录,删除对应项目的缓存文件夹 - 重启Xcode后重新构建运行
- 按快捷键
全局强制兜底方案
如果以上方法都不生效,可在AppDelegate中添加全局方向控制逻辑,该配置优先级最高,可直接覆盖所有其他设置:
// Swift 示例,添加到AppDelegate中 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return .portrait }
// Objective-C 示例,添加到AppDelegate.m中 - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }
内容的提问来源于stack exchange,提问作者drago
相关产品推荐
相关产品推荐

