How to set TabBarIcon color correctly in SwiftUI since .accentColor is deprecated?

219 Views Asked by At

It seems quite hard to change the color of the selected UITabBarItem in SwiftUI. To define the design at central place in the app, I tried to use .appearance() to do this, but nothing worked:

// Only effects the unselected items
UITabBar.appearance().unselectedItemTintColor = .red

// No effect, deprecated in iOS 8(!)
UITabBar.appearance().selectedImageTintColor = .cyan

// Only effects the title, not the icon 
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.green], for: .normal)

// No effect
UITabBar.appearance().tintColor = .red

The only thing that works is to not use the appearance proxy but setting the color "manually" in the SwiftUI View definition using .accentColor

struct MainView: View {
    var body: some View {
        TabView {
             SomeView()
                .tabItem {
                    Label("Main", systemImage: "list.dash")
                        .tint(.green) // No effect
                }
                .tint(.yellow) // No effect
        }
        .accentColor(.orange)
        
        // No effect
        //.tint(.red)
    }
}

However, .accentColor will also be deprecated:

'accentColor' will be deprecated in a future version of iOS: Use the assets catalog's accent color or View.tint(_:) instead

Using .tint on the TabView has no effect. UITabBarItem has no tint appearance property.

To how to correctly set the icon color without using accentColor (preferably via the appearance)

0

There are 0 best solutions below