.navigationBarTitle("") and .navigationBarHidden(true) to the view inside NavigationView. The title modifier must be present for the hidden modifier to take effect. For reusable code, wrap both in a ViewModifier extension.Why this happens
SwiftUI’s NavigationView reserves space for a navigation bar by default, even when you intend to hide it. The layout engine measures the bar area before applying visibility modifiers, so calling .navigationBarHidden(true) alone leaves the reserved space intact. This behavior stems from how SwiftUI resolves navigation bar preferences up the view hierarchy — the bar’s height is calculated during layout, and hiding it afterward doesn’t collapse the space.
Additionally, the navigation bar title preference influences whether the bar is considered “present” for layout purposes. Without an explicit title (even an empty one), SwiftUI treats the bar as having default content, preventing the hide modifier from collapsing the reserved area.
How to fix it
1. Add empty title with hidden modifier (simplest fix)
Apply both modifiers to the child view inside NavigationView. The empty title tells the layout system the bar has no content, allowing .navigationBarHidden(true) to collapse the space.
NavigationView {
FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL))
.navigationBarTitle("")
.navigationBarHidden(true)
}
Order doesn’t matter, but both must be present. This works for the initial view where you want no bar at all.
2. Create a reusable ViewModifier
Encapsulate the two modifiers in a ViewModifier and expose it via a View extension. This keeps call sites clean and ensures consistency across the project.
struct HiddenNavigationBar: ViewModifier {
func body(content: Content) -> some View {
content
.navigationBarTitle("", displayMode: .inline)
.navigationBarHidden(true)
}
}
extension View {
func hiddenNavigationBarStyle() -> some View {
modifier(HiddenNavigationBar())
}
}
Usage:
NavigationView {
VStack {
Spacer()
HStack {
Spacer()
Text("Hello World!")
Spacer()
}
Spacer()
}
.padding()
.background(Color.green)
.hiddenNavigationBarStyle()
}
3. Handle navigation stack visibility changes
When pushing to deeper views that should show a bar, the root view’s hidden preference can persist incorrectly. A workaround uses a @State/@Binding pair to toggle the root view’s preference on appear/disappear.
struct RootView: View {
@State private var isNavigationBarHidden = true
var body: some View {
NavigationView {
ZStack {
Color.red
NavigationLink("Detail", destination: DetailView(isNavigationBarHidden: $isNavigationBarHidden))
}
.navigationBarTitle("")
.navigationBarHidden(isNavigationBarHidden)
.onAppear { isNavigationBarHidden = true }
}
}
}
struct DetailView: View {
@Binding var isNavigationBarHidden: Bool
var body: some View {
Color.green
.navigationBarTitle("Detail")
.onAppear { isNavigationBarHidden = false }
.onDisappear { isNavigationBarHidden = true }
}
}
This binding approach forces the root view to update its preference when child views appear or disappear. It’s a workaround for a known SwiftUI limitation where navigationBarHidden set to false deeper in the stack doesn’t reliably override the ancestor’s true setting.
How to prevent it
- Always pair
.navigationBarTitle("")with.navigationBarHidden(true)when you want zero bar space. - Use the
hiddenNavigationBarStyle()modifier for consistency and easier maintenance. - Test navigation flows on device — simulator layout sometimes differs.
- Prefer
NavigationStack(iOS 16+) for new projects; it handles bar visibility more predictably.
FAQ
Does this work with NavigationStack in iOS 16+?
NavigationStack uses toolbarVisibility(.hidden, for: .navigationBar) instead. The old modifiers still compile but are deprecated. Migrate to the new API for future-proof code.
Why does the space return when I rotate the device?
Layout recalculates on rotation. Ensure both modifiers are applied to the same view that holds the List or content, not a parent container. The ViewModifier approach guarantees this.
Can I animate the bar hiding/showing?
SwiftUI doesn’t animate navigationBarHidden changes. The binding workaround updates state instantly. For custom animations, consider a manual overlay approach instead of the system bar.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
