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

SwiftUI中如何修改TabView选中项的颜色?

SwiftUI TabBar 选中项颜色修改问题

我已成功修改TabBar的背景色和未选中项的颜色,但无法修改选中项的颜色。

我的视图代码如下:

var body: some View {
    TabView {
        CountriesView(homeViewModel: homeViewModel)
            .tabItem {
                Label {
                    Text("Home")
                } icon: {
                    Image(systemName: "house.fill")
                }
            }
            .onAppear {
                homeViewModel.getCountries()
            }

        SavedCountriesView(homeViewModel: homeViewModel)
            .tabItem {
                Label {
                    Text("Saved")
                } icon: {
                    Image(systemName: "heart.fill")
                }
            }
    }
    .onAppear {
        UITabBar.appearance().backgroundColor = UIColor.gray
        UITabBar.appearance().unselectedItemTintColor = UIColor.white
        UITabBar.appearance().barTintColor = UIColor(Color.black)
    }
}

我已尝试以下方案:

  • 修改UITabBar的barTintColor属性;
  • 给tabItem内的文本和图片添加foregroundColor,无效果;
  • 修改TabView的accentColor(该属性已在后续版本废弃),但此操作会影响整个页面,我仅想单独修改TabBar选中项的颜色。

解决方法

方法1:使用UIKit的tintColor属性

通过设置UITabBar.appearance().tintColor可以单独控制TabBar选中项的颜色,不会影响页面其他元素:

var body: some View {
    TabView {
        CountriesView(homeViewModel: homeViewModel)
            .tabItem {
                Label {
                    Text("Home")
                } icon: {
                    Image(systemName: "house.fill")
                }
            }
            .onAppear {
                homeViewModel.getCountries()
            }

        SavedCountriesView(homeViewModel: homeViewModel)
            .tabItem {
                Label {
                    Text("Saved")
                } icon: {
                    Image(systemName: "heart.fill")
                }
            }
    }
    .onAppear {
        UITabBar.appearance().backgroundColor = UIColor.gray
        UITabBar.appearance().unselectedItemTintColor = UIColor.white
        // 设置选中项颜色,替换为你需要的颜色
        UITabBar.appearance().tintColor = UIColor.orange
    }
}

方法2:使用SwiftUI原生tint修饰符(推荐)

在iOS 15+版本中,可以直接给TabView添加tint修饰符,该修饰符只会作用于TabBar的选中项,不会影响子视图(子视图可单独设置自己的色调):

var body: some View {
    TabView {
        CountriesView(homeViewModel: homeViewModel)
            .tabItem {
                Label {
                    Text("Home")
                } icon: {
                    Image(systemName: "house.fill")
                }
            }
            .onAppear {
                homeViewModel.getCountries()
            }

        SavedCountriesView(homeViewModel: homeViewModel)
            .tabItem {
                Label {
                    Text("Saved")
                } icon: {
                    Image(systemName: "heart.fill")
                }
            }
    }
    .tint(.orange) // 仅控制TabBar选中项颜色
    .onAppear {
        UITabBar.appearance().backgroundColor = UIColor.gray
        UITabBar.appearance().unselectedItemTintColor = UIColor.white
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 00:55:26