Xcode中iPhone X如何恢复16:9上下黑边布局?
解决iOS应用在全面屏设备上恢复16:9显示比例的方案
嘿,我刚好处理过类似的问题,给你几个靠谱的解决方案,帮你把界面拉回16:9的比例,解决iPhone X及以后全面屏的显示异常:
方法一:通过启动屏幕(Launch Screen)约束实现(推荐)
这是最稳妥的系统级解决方案,不需要写额外代码:
- 打开项目中的
Launch Screen.storyboard - 拖一个
UIView作为根容器添加到画布中 - 给这个容器添加约束:
- 水平居中对齐父视图
- 垂直居中对齐父视图
- 设置宽高比为
16:9(竖屏场景用9:16,根据你的应用方向调整) - 添加宽度不超过父视图宽度、高度不超过父视图高度的约束,避免在非全面屏设备上出现拉伸
- 把你原来的启动界面内容全部放到这个容器里
这样启动后,系统会自动将整个应用的内容限制在这个16:9的容器内,全面屏设备上自然会出现上下黑边,完美适配传统比例。
方法二:代码层面调整Window的Frame
如果需要更灵活的控制,可以在AppDelegate或SceneDelegate中手动设置窗口尺寸:
// 针对AppDelegate的情况 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { guard let window = window else { return true } let screenSize = UIScreen.main.bounds.size // 计算16:9比例的目标高度(竖屏场景:高度 = 宽度 * 9/16) let targetHeight = screenSize.width * 9 / 16 // 计算上下黑边的间距 let topOffset = (screenSize.height - targetHeight) / 2 // 重新设置window的frame window.frame = CGRect(x: 0, y: topOffset, width: screenSize.width, height: targetHeight) return true } // 针对SceneDelegate的情况 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } let window = UIWindow(windowScene: windowScene) let screenSize = windowScene.screen.bounds.size let targetHeight = screenSize.width * 9 / 16 let topOffset = (screenSize.height - targetHeight) / 2 window.frame = CGRect(x: 0, y: topOffset, width: screenSize.width, height: targetHeight) self.window = window window.makeKeyAndVisible() }
注意:如果你的应用使用Auto Layout,要确保所有子视图的约束都是基于window的frame,而不是直接绑定到屏幕边界。
方法三:Info.plist配置辅助
可以配合上面的方法,在Info.plist中添加以下配置增强效果:
- 添加
UIRequiresFullScreen,设置值为NO,允许应用不全屏显示 - 确保
UISupportedInterfaceOrientations只包含你需要的方向(比如竖屏场景只保留UIInterfaceOrientationPortrait)
内容的提问来源于stack exchange,提问作者Dennis Bonzanni
相关产品推荐
相关产品推荐

