iOS15搭配Xcode13环境下导航栏与标签栏变黑如何恢复旧样式?
这是Xcode 13使用iOS 15 SDK编译的默认行为变更:iOS 15为UINavigationBar、UITabBar新增了scrollEdgeAppearance属性,默认采用透明背景配置,当界面没有内容滚动到栏的下方区域时,栏会显示为完全透明,你看到的黑色实际是当前window根视图的背景色透出来的效果。
你可以通过以下代码统一恢复到Xcode 12及更早版本的显示样式:
全局适配方案(推荐)
在App启动入口application(_:didFinishLaunchingWithOptions:)中添加全局外观配置:
Swift 代码
if #available(iOS 15.0, *) { // 适配导航栏 let navAppearance = UINavigationBarAppearance() navAppearance.configureWithOpaqueBackground() // 可自定义背景色、标题样式等 // navAppearance.backgroundColor = .systemBackground // navAppearance.titleTextAttributes = [.foregroundColor: UIColor.label] UINavigationBar.appearance().standardAppearance = navAppearance UINavigationBar.appearance().scrollEdgeAppearance = navAppearance // 适配标签栏 let tabAppearance = UITabBarAppearance() tabAppearance.configureWithOpaqueBackground() // 可自定义背景色、文字样式等 // tabAppearance.backgroundColor = .systemBackground UITabBar.appearance().standardAppearance = tabAppearance UITabBar.appearance().scrollEdgeAppearance = tabAppearance }
Objective-C 代码
if (@available(iOS 15.0, *)) { // 适配导航栏 UINavigationBarAppearance *navAppearance = [[UINavigationBarAppearance alloc] init]; [navAppearance configureWithOpaqueBackground]; // 自定义示例 // navAppearance.backgroundColor = [UIColor systemBackgroundColor]; // navAppearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor labelColor]}; [UINavigationBar appearance].standardAppearance = navAppearance; [UINavigationBar appearance].scrollEdgeAppearance = navAppearance; // 适配标签栏 UITabBarAppearance *tabAppearance = [[UITabBarAppearance alloc] init]; [tabAppearance configureWithOpaqueBackground]; // 自定义示例 // tabAppearance.backgroundColor = [UIColor systemBackgroundColor]; [UITabBar appearance].standardAppearance = tabAppearance; [UITabBar appearance].scrollEdgeAppearance = tabAppearance; }
单页面单独适配方案
如果不需要全局统一修改,仅需要调整个别页面的显示,在对应页面的viewDidLoad方法中单独设置当前控制器关联的栏外观即可。
效果验证
上述配置仅作用于iOS 15及以上版本,不会影响更低版本系统的原有显示逻辑,配置完成后重新编译运行即可恢复旧版本的不透明栏样式。
问题对应截图参考


内容的提问来源于stack exchange,提问作者Zim
相关产品推荐
相关产品推荐

