WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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!

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.