Search code examples
swiftswiftui

Why does changing unselected tab color in Swift UI doesnt work through appearence anymore?


so I am trying to change the unselected tab color in a tabview in SwiftUI, however, by doing this in the init: UITabBar.appearance().unselectedItemTintColor = UIColor.white Does not work, as it used to.

Does anyone know what happened and what I can do to work around this now that it doesnt work anymore? I tried doing:



 TabView(selection: $selectedTab) {
            SettingsView()
                .tabItem {
                    Image("ic-settings")
                        .renderingMode(.template)
                        .foregroundColor(selectedTab == "settings" ? Color("AccentColor") : Color.white)
                }
                .tag("settings")

            NavigationView {
                ChatView()
            }
            .tabItem {
                Image("ic-chat-bubble")
                    .renderingMode(.template)
                    .foregroundColor(selectedTab == "chat" ? Color("AccentColor") : Color.white)
            }

But this also made no difference, and the unselected tab icon are still the default grey from .template rendering.

I tried doing What is up there, and I also tried using TabBarAccessor, but it also seem that it is deprecated in most recent version of XCODE. I just want to make the unselected icons in the tab view White/original color, while maintaining the AccentColor, for the selected tab


Solution

  • Using UITabBar.appearance().unselectedItemTintColor works for me.

    If you want more control, see Change color of unselected icon in TabView (SwiftUI) , the answer to your previous question.

    Example code:

    struct ContentView: View {
        @State private var selectedTab = "chat"
    
        init() {
             UITabBar.appearance().unselectedItemTintColor = UIColor.green
        }
    
        var body: some View {
            TabView(selection: $selectedTab) {
                Text("SettingView")
                    .tabItem {
                        Image(systemName: "globe")
                            .renderingMode(.template)
                            .foregroundColor(selectedTab == "settings" ? Color.green : Color.white)
                    }
                    .tag("settings")
    
                NavigationView {
                  //  ChatView()
                    Text("ChatView")
                }
                .tabItem {
                    Image(systemName: "house")
                        .renderingMode(.template)
                        .foregroundColor(selectedTab == "chat" ? Color.green : Color.white)
                }
                .tag("chat")
                
                Text("AnalyticsView")
             //   AnalyticsView()
                    .tabItem {
                        Image(systemName: "info")
                            .renderingMode(.template)
                            .foregroundColor(selectedTab == "analytics" ? Color.green : Color.white)
                    }
                    .tag("analytics")
            }
        }
    }