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

iOS TabBar背景色莫名变更,咨询自定义设置方法及原因

TabBar背景色自定义问题解决

问题根源

你写的代码完全搞错了对象——你在修改UINavigationBar(导航栏)的外观,而不是UITabBar(标签栏)的。这就导致你的设置根本没作用到目标控件上,点击切换Tab后,系统默认的TabBar样式就会显现,所以背景色才会莫名变化。

是否支持自定义

当然支持,不管是iOS 15还是更早版本,都能完全自定义TabBar的背景色。

正确实现方式

方式一:全局统一配置(AppDelegate中)

把你的代码改成针对UITabBar的设置,同时注意iOS 15+需要额外配置scrollEdgeAppearance,否则滑动页面时TabBar样式可能会变:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let tabBarStyle = UITabBarAppearance()
    tabBarStyle.configureWithOpaqueBackground() // 自动设置isTranslucent为false
    tabBarStyle.backgroundColor = UIColor(red: 0.42, green: 0.5, blue: 0.2, alpha: 1)
    
    UITabBar.appearance().standardAppearance = tabBarStyle
    if #available(iOS 15.0, *) {
        UITabBar.appearance().scrollEdgeAppearance = tabBarStyle
    }
    
    return true
}

方式二:单个TabBarController单独配置

如果不想全局修改,也可以在对应的TabBarController的viewDidLoad里设置:

override func viewDidLoad() {
    super.viewDidLoad()
    
    let tabBarStyle = UITabBarAppearance()
    tabBarStyle.configureWithOpaqueBackground()
    tabBarStyle.backgroundColor = UIColor(red: 0.42, green: 0.5, blue: 0.2, alpha: 1)
    
    tabBar.standardAppearance = tabBarStyle
    if #available(iOS 15.0, *) {
        tabBar.scrollEdgeAppearance = tabBarStyle
    }
}

额外提示

  • 不要手动设置isTranslucent,configureWithOpaqueBackground()已经帮你把这个属性设为false了,避免背景色和内容区域混合。
  • 既然你没在storyboard里做任何设置,用上面的代码就能完全覆盖系统默认样式,切换Tab时不会再出现背景色异常变化的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 00:45:00