首页 > 解决方案 > 在 TabBar SwiftUI 中更改选项卡选择颜色

问题描述

我正在尝试更改 TabBar 中选定选项卡的颜色,但没有任何效果。backgroundColor我可以通过编写 来更改 TabBar

struct ContentView: View {
    init() {
        UITabBar.appearance().backgroundColor = UIColor.purple
    }
    var body: some View { 
    }
}

在swift中,我们设置tintColor并且它确实改变了选定选项卡的颜色。但是我需要为 swiftUI 做什么?

这是我的代码,

    TabView(selection: $selection) {
        AView()
            .tabItem {
                VStack {
                    Image(systemName: "bubble.left.and.bubble.right")
                    Text("A Tab")
                }
        }.tag(0)

        BView()
            .tabItem {
                VStack {
                    Image(systemName: "house")
                    Text("B Tab")
                }
        }.tag(1)

        CView()
            .tabItem {
                VStack {
                    Image(systemName: "circle.grid.3x3")
                    Text("C Tab")
                }
        }.tag(2)
    }

有什么帮助吗?提前致谢!!

标签: iosswiftuitabbar

解决方案


使用accentColorhttps ://developer.apple.com/documentation/swiftui/tabview/3368073-accentcolor

TabView {
  // fill this out with your tabbed content
}
.accentColor(.orange)

在此处输入图像描述


推荐阅读