0

How do I change my TabBar's color/tint in SwiftUI? I can set it once on init using the following:

init() {
    UITabBar.appearance().barTintColor = .green
}

var body: some View {
        TabView {
            ...

but later, when a button is pressed, I want to change the color to something different. Simply updating the appearance() of UITabBar like above has no effect. I'm thinking I need to somehow "commit" my changes or mark the TabBar as needing to be updated, but I'm not sure how this would be done. Thanks

tallen11
  • 1,387
  • 2
  • 17
  • 33

2 Answers2

0

This question answered my question as well: question

The solution was to force the TabBar to entirely reload when I wanted the color to change. I did this by settings its ID to something that would change when I wanted the color to change, so when that value changed, the ID of the TabBar changed and the whole TabBar gets reloaded.

tallen11
  • 1,387
  • 2
  • 17
  • 33
0
struct TabBar: View {
    init() {
        UITabBar.appearance().barTintColor = UIcolor.black

    var body: some View {
        TabView {
            HomeView().tabItem {
                Image(systemName: "house.fill")
                Text("Home")
            }
            MapView().tabItem {
                Image(systemName: "mappin.circle.fill")
                Text("Map")
            }
        }
        .edgesIgnoringSafeArea(.top)
    }
}

I found something like this. Here is the reference https://www.thetopsites.net/article/58250211.shtml