Change the tab selection color in TabBar SwiftUI

2020-06-04 04:26发布

问题:

I am trying to change the color of selected tab in TabBar, but nothing worked. I can change the TabBar backgroundColor by writing

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

In swift, we set tintColor and it does change the color of selected tab. But what do i need to do for swiftUI?

Here is my code,

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

Any help with this? Thanks in advance!!

回答1:

Use accentColor: https://developer.apple.com/documentation/swiftui/tabview/3368073-accentcolor

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