iOS 15下TabBar Item文本颜色显示异常问题求助
iOS 15 TabBar 常态文本颜色过浅问题修复
针对你遇到的iOS15下TabBar常态文本颜色过浅的问题,可通过调整UITabBarAppearance的属性设置来解决,修改后的代码如下:
func updateTabBarItemFont() { self.tabBar.tintColor = UIColor.tabBarForegroundActive self.tabBar.barTintColor = UIColor.white self.tabBar.isTranslucent = false let textStyle = TextStyle.tabBarHeader let normalAttrs = [NSAttributedString.Key.font: textStyle.font, NSAttributedString.Key.foregroundColor: UIColor.neutralForegroundNormal] let selectedAttrs = [NSAttributedString.Key.font: textStyle.font, NSAttributedString.Key.foregroundColor: UIColor.tabBarForegroundActive] if #available(iOS 15.0, *) { let appearance = UITabBarAppearance() appearance.configureWithOpaqueBackground() // 全局设置文本属性,覆盖所有布局样式 appearance.titleTextAttributes = normalAttrs appearance.selectedTitleTextAttributes = selectedAttrs // 兜底设置堆叠布局的文本属性 appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttrs appearance.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttrs self.tabBar.standardAppearance = appearance // 为当前TabBar实例设置滚动边缘样式,而非全局设置 self.tabBar.scrollEdgeAppearance = appearance } else { UITabBarItem.appearance().setTitleTextAttributes(normalAttrs, for: .normal) UITabBarItem.appearance().setTitleTextAttributes(selectedAttrs, for: .selected) } }
关键改动说明
- 全局文本属性覆盖:iOS15中仅设置
stackedLayoutAppearance可能无法完全覆盖所有场景的默认样式,直接给UITabBarAppearance的titleTextAttributes和selectedTitleTextAttributes赋值,能确保所有布局模式(堆叠/内联/紧凑)下的文本属性都遵循自定义设置,避免系统默认浅灰色生效。 - 滚动边缘样式绑定到当前实例:将
scrollEdgeAppearance的设置从全局UITabBar.appearance()改为当前TabBar实例,避免全局样式冲突,确保页面滚动到边缘时TabBar仍保持自定义样式。 - 兜底设置堆叠布局属性:保留
stackedLayoutAppearance的属性设置,确保最常用的堆叠布局下文本样式完全符合预期。
额外排查点
如果修改后仍有问题,检查UIColor.neutralForegroundNormal的颜色定义,确认该颜色在iOS15的动态颜色适配中未被自动调整(比如未正确配置深色/浅色模式下的颜色值)。
内容的提问来源于stack exchange,提问作者Jayaseelan Kumarasamy
相关产品推荐
相关产品推荐

