Skip to main content

How to customize the background color of navigation bars, tab bars, and toolbars

About 3 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to customize the background color of navigation bars, tab bars, and toolbars 관련

SwiftUI by Example

Back to Home

How to customize the background color of navigation bars, tab bars, and toolbars | SwiftUI by Example

How to customize the background color of navigation bars, tab bars, and toolbars

Updated for Xcode 15

New in iOS 16

SwiftUI's toolbarBackground() modifier lets us customize the way toolbars look in our app, controlling the styling of NavigationStack, TabView, and other toolbars as needed.

For example, this shows a list of 100 rows using a teal background color for the navigation bar:

NavigationStack {
    List(0..<100) {
        Text("Row \($0)")
    }
    .navigationTitle("100 Rows")
    .toolbarBackground(.teal)
}

Download this as an Xcode projectopen in new window

Important

The background you choose here is used when the system deems it necessary, rather than always. So, in the code above the navigation stack view will appear without the color at first, but will change color as soon as the list scrolls under the navigation bar.

Using toolbarBackground(.teal) doesn't specify which toolbar should be colored teal, so it's down to the system to select whatever is the primary toolbar – that's the navigation bar on iOS, but on macOS it will be the window toolbar instead.

If you want one or two bar types to be colored, or perhaps if you want to provide different styling for each bar, you can provide a second parameter to toolbarBackground() to get extra control. For example, we could ask the system to color both the tab bar and the navigation bar like this:

TabView {
    NavigationStack {
        List(0..<100) {
            Text("Row \($0)")
        }
        .navigationTitle("100 Rows")
        .toolbarBackground(.orange, for: .navigationBar, .tabBar)
    }
    .tabItem {
        Label("Rows", systemImage: "list.bullet")
    }
}

Download this as an Xcode projectopen in new window

This modifier has one other important use: rather than specify a background color, you can ask the system to hide the background entirely, like this:

NavigationStack {
    List(0..<100) {
        Text("Row \($0)")
    }
    .navigationTitle("100 Rows")
    .toolbarBackground(.hidden)
}

Download this as an Xcode projectopen in new window

In that example, the list content will appear directly alongside the navigation title as the user scrolls. If you take this approach, please ensure your main content and toolbar content don't clash when they overlap!

Similar solutions…
How to hide the tab bar, navigation bar, or other toolbars | SwiftUI by Example

How to hide the tab bar, navigation bar, or other toolbars
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks
All SwiftUI property wrappers explained and compared | SwiftUI by Example

All SwiftUI property wrappers explained and compared
How to embed views in a tab bar using TabView | SwiftUI by Example

How to embed views in a tab bar using TabView
How to change the background color of List, TextEditor, and more | SwiftUI by Example

How to change the background color of List, TextEditor, and more

이찬희 (MarkiiimarK)
Never Stop Learning.