10

Any idea on how to apply a specific background color to the bottom toolbar?

NavigationView {
    List {
        ....
    }
    .toolbar {
        ToolbarItem(placement: .bottomBar) {
            Button(action: { model.selectTab(tab: "ITEM1") }, label: { Text("ITEM1") })
        }
        ToolbarItem(placement: .bottomBar) {
            Button(action: { model.selectTab(tab: "ITEM2") }, label: { Text("ITEM2") })
        }
        ToolbarItem(placement: .bottomBar) {
            Button(action: { model.selectTab(tab: "ITEM3") }, label: { Text("ITEM3") })
        }
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
Daniele B
  • 19,801
  • 29
  • 115
  • 173

5 Answers5

12

iOS 16 Onwards

enter image description here

Note: use .toolbarBackground(.visible, for: .navigationBar) The preferred visibility of the background of the bar.

Detail tutorial here - https://janeshswift.com/ios/swiftui/how-to-change-navigationview-colour-in-swiftui/

struct ContentView : View {
    var body: some View {
        NavigationStack {
            Text("Dashboard")
                .toolbar {
                    ToolbarItem(placement: .principal) {
                        Text("Cool Title")
                            .foregroundColor(.black)
                    }
                }
                .navigationBarTitleDisplayMode(.inline)
                .toolbarBackground(.red, for: .navigationBar)
                .toolbarBackground(.visible, for: .navigationBar)
        }
    }
}
Jack
  • 13,571
  • 6
  • 76
  • 98
5

iOS 16+

.toolbarBackground(
    .red,
    for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)

For more info:

https://developer.apple.com/documentation/swiftui/tabview/toolbarbackground(_:for:)

Arturo
  • 3,254
  • 2
  • 22
  • 61
  • 2
    For .toolbar not working. – Ramis Sep 25 '22 at 19:42
  • Are you using iOS 16? See the documentation above @Ramis – Arturo Sep 26 '22 at 21:02
  • 1
    All of these comments are assuming the "normal" toolbars. .bottomBar doesn't seem to respond except to UIToolbar.appearance().barTintColor = UIColor.red. @Arturo, your suggestion works if you want to colour the navigation bar (the top) or the TabBar (the very bottom). But there is frustrating little control over the addition toolbar .bottomBar – Tomm P Mar 08 '23 at 14:47
4

The code below doesn't work for me:

init() {
    UIToolbar.appearance().barTintColor = UIColor.red
}

This code works:

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

Taken from Kristaps Grinbergs' blog.

2

You can do this using UIToolbar appearance. Tested with Xcode 12 / iOS 14.

demo

struct DemoView: View {
    
    init() {
        UIToolbar.appearance().barTintColor = UIColor.red
    }
    
    var body: some View {
        NavigationView {
            List {
                Text("Item")
            }
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    Button(action: { }, label: {Text("ITEM1")})
                }
                ToolbarItem(placement: .bottomBar) {
                    Button(action: { }, label: {Text("ITEM2")})
                }
                ToolbarItem(placement: .bottomBar) {
                    Button(action: { }, label: {Text("ITEM3")})
                }
            }
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
0

Here is bit hacky solution that avoids overriding UIToolbar.appearance() in the app. It can work for both Nav and Tab bar, or only for the one you choose (see this answer for NavBar colouring only).

struct NavWithBackground: View {
    var body: some View {
        NavigationView {
            List(0..<100) {
                Text("Row \($0)")
            }
            .padding(.vertical, 1) // note top 1 padding!
            .background(.red)
            .toolbar {
                ToolbarItemGroup(placement: .bottomBar) {
                    Button(action: {}, label: { Text("ITEM1") })
                    Button(action: { }, label: { Text("ITEM2") })
                    Button(action: { }, label: { Text("ITEM3") })
                }
            }
            .navigationTitle("100 Rows")
        }
    }
}
Paul B
  • 3,989
  • 33
  • 46