Skip to main content

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

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

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

SwiftUI by Example

Back to Home

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

Updated for Xcode 15

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 projectopen in new window

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 projectopen in new window

Similar solutions…
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
How to hide the home indicator and other system UI | SwiftUI by Example

How to hide the home indicator and other system UI
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 layer views on top of each other using ZStack | SwiftUI by Example

How to layer views on top of each other using ZStack
How to show different images and other views in light or dark mode | SwiftUI by Example

How to show different images and other views in light or dark mode

이찬희 (MarkiiimarK)
Never Stop Learning.