How to hide a Navigation Back button in SwiftUI
Table of Contents
How to hide a Navigation Back button in SwiftUI
To hide a navigation back button in SwiftUI, we apply .navigationBarBackButtonHidden(true)
modifier to the view that you want to hide the back button.
In this example, we set .navigationBarBackButtonHidden(true)
to the DetailView
.
NavigationStack {
NavigationLink("Detail") {
DetailView()
.navigationBarBackButtonHidden(true) // 1
}
}
1 Apply .navigationBarBackButtonHidden(true)
to the DetailView
.
Adding .navigationBarBackButtonHidden(true)
will cause two things to happen.
- The back button will be hidden on that view.
- The edge swipe to go back will be disable.
In summary, you not just hide the UI, but you disable every function to go back.
As a result, you can't go back once you land on the DetailView
.
You can easily support sarunw.com by checking out this sponsor.
Debug 10x faster with Proxyman: Your ultimate tool to capture HTTPs requests/ responses, natively built for your iPhone and macOS. Special deal for Black Friday: Get 30% off for all Proxyman licenses with code “BLACKFRIDAY2024”.
When should you hide a back button
A navigation view is a common way of navigation in iOS.
People expect they can always go back with a navigation view. So, you better have a good reason preventing people not to go back.
There is no rule of when to use navigationBarBackButtonHidden(true)
, but you probably want to use it when it is the end of the user flow.
For example, you use a navigation view for a cart checkout flow, and once the order is made, there is no use in going back.
In that case, you can hide the back button and provide another action to navigate the user to where it makes sense for your app, e.g., an order status.
Read more article about SwiftUI or see all available topic
Enjoy the read?
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Become a patron Buy me a coffee Tweet ShareContentUnavailableView in SwiftUI
A dedicated SwiftUI view to present an empty state for your app.
Default Value in Swift Dictionary
Learn how to set and get value from a dictionary with a default value.