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

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)
    }
}

关键改动说明

  1. 全局文本属性覆盖:iOS15中仅设置stackedLayoutAppearance可能无法完全覆盖所有场景的默认样式,直接给UITabBarAppearance的titleTextAttributes和selectedTitleTextAttributes赋值,能确保所有布局模式(堆叠/内联/紧凑)下的文本属性都遵循自定义设置,避免系统默认浅灰色生效。
  2. 滚动边缘样式绑定到当前实例:将scrollEdgeAppearance的设置从全局UITabBar.appearance()改为当前TabBar实例,避免全局样式冲突,确保页面滚动到边缘时TabBar仍保持自定义样式。
  3. 兜底设置堆叠布局属性:保留stackedLayoutAppearance的属性设置,确保最常用的堆叠布局下文本样式完全符合预期。

额外排查点

如果修改后仍有问题,检查UIColor.neutralForegroundNormal的颜色定义,确认该颜色在iOS15的动态颜色适配中未被自动调整(比如未正确配置深色/浅色模式下的颜色值)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 19:33:32