UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I change the background color of my bottom navigation bar

Forums > SwiftUI

I have this navigation tool bar at the bottom of my screen and I can't figure out how to change the backfround of it to light grey. Any advice?

          // Bottom Navigation
                    ToolbarItemGroup(placement: .bottomBar) {

                        Button(action: {
                            print("mob: Performance tapped")
                        }, label: {
                           Label("Performance", systemImage: "chart.line.uptrend.xyaxis")
                               .labelStyle(VerticalLabelStyle())
                               .frame(maxWidth: .infinity)
                        })
                        Spacer()

                        Button(action: {
                            print("mob: Funnel tapped")
                            showFunnel = true
                        }, label: {
                           Label("Funnel", systemImage: "arrowtriangle.down.fill")
                              .labelStyle(VerticalLabelStyle())
                              .frame(maxWidth: .infinity)
                        })
                        Spacer()

                        Button(action: {
                            print("mob: AB tapped")
                            showAB = true

                        }, label: {
                           Label("AB", systemImage: "list.bullet.rectangle.fill")
                               .labelStyle(VerticalLabelStyle())
                               .frame(maxWidth: .infinity)
                        })
                        Spacer()

                        Button(action: {
                            print("mob: KW tapped")
                            showKW = true

                        }, label: {
                           Label("KW", systemImage: "text.alignleft")
                              .labelStyle(VerticalLabelStyle())
                        })
                    }

2      

this is a very good question, i have struggled with this as well, some have suggested to use

init() {
UITabBar.appearance().backgroundColor = UIColor.red
}

but they are more like UIKIT based and also do not work in all cases, i wll look for a answer to this one for sure

2      

Thank you for responding Amit. Sadly this didn't work. I see where you are coming from. I used an init to modify the top toolbar and that work, see below. But I cant seem to get the bottom nav bar changed.

    init() {
      let coloredAppearance = UINavigationBarAppearance()
      coloredAppearance.configureWithOpaqueBackground()
      coloredAppearance.backgroundColor = .systemGray4
      coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
      coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.systemGray]
      UINavigationBar.appearance().standardAppearance = coloredAppearance
      UINavigationBar.appearance().compactAppearance = coloredAppearance
      UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
      UINavigationBar.appearance().tintColor = .white
    } 

2      

This compiles but does not work

    init() {
        let tabBarAppearance: UITabBarAppearance = UITabBarAppearance()
        tabBarAppearance.backgroundColor = .systemRed
    }

2      

Try this

struct ContentView: View {
    var body: some View {
        TabView {
            Text("Home")
                .tabItem {
                    Label("Home", systemImage: "house")
                }
            Text("Search")
                .tabItem {
                    Label("Search", systemImage: "magnifyingglass")
                }
            Text("Notification")
                .tabItem {
                    Label("Notification", systemImage: "bell")
                }
            Text("Settings")
                .tabItem {
                    Label("Settings", systemImage: "gearshape")
                }
        }
        .tint(.white) // <- change the color of each tab icon
        .onAppear {
            let tabBarAppearance = UITabBarAppearance()
            tabBarAppearance.backgroundColor = .red
            UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
        }
    }
}

I have been looking at sarunw blog How to change TabView color in SwiftUI and this should work in iOS16 but it did not for me. maybe a bug.

struct ContentView: View {
    var body: some View {
        TabView {
            Group {
                Text("Home")
                    .tabItem {
                        Label("Home", systemImage: "house")
                    }
                Text("Search")
                    .tabItem {
                        Label("Search", systemImage: "magnifyingglass")
                    }
                Text("Notification")
                    .tabItem {
                        Label("Notification", systemImage: "bell")
                    }
                Text("Settings")
                    .tabItem {
                        Label("Settings", systemImage: "gearshape")
                    }
            }
            .toolbar(.visible, for: .tabBar)
            .toolbarBackground(Color.yellow, for: .tabBar)
        }
    }
}

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.