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

How to hide the tab bar, navigation bar, or other toolbars

Paul Hudson    @twostraws   

Updated for Xcode 14.2

New in iOS 16

SwiftUI’s toolbar() modifier lets us hide or show any of the system bars whenever we need, which is particularly useful when you have a TabView that you want to hide after a navigation push.

Attach the modifier to whatever view should trigger the bar to be hidden or shown. For example, this code will cause the tab bar to be hidden when it’s pushed onto the navigation stack:

TabView {
    NavigationStack {
        NavigationLink("Tap Me") {
            Text("Detail View")
                .toolbar(.hidden, for: .tabBar)
        }
        .navigationTitle("Primary View")
    }
    .tabItem {
        Label("Home", systemImage: "house")
    }
}

Download this as an Xcode project

If you don’t specify an exact bar to hide – if you write just toolbar(.hidden) without specifying for: .tabBar – the hide request flows upwards to the nearest container. In this case it will result in the navigation bar being hidden as that’s the nearest container.

You can change the value passed in to toolbar() whenever you want, so you could allow the user to toggle the navigation bar with code like this:

struct DetailView: View {
    @State private var showNavigationBar = true

    var body: some View {
        Text("Detail View")
            .toolbar(showNavigationBar ? .visible : .hidden)
            .onTapGesture {
                withAnimation {
                    showNavigationBar.toggle()
                }
            }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationStack {
            NavigationLink("Tap Me", destination: DetailView.init)
            .navigationTitle("Primary View")
        }
    }
}

Download this as an Xcode project

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: 3.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.