NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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

Paul Hudson    @twostraws   

Updated for Xcode 14.2

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 project

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 project

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 project

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!

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.