SwiftUI “Back” Button Woes? Customize It!
3 min read#Navigation#iOS#SwiftUI

SwiftUI is an amazing framework that lets us create stunning views with ease. Features like lists, forms, and buttons are simple and straightforward to implement compared to what we’re used to in UIKit. However, customizing the navigation back button has always been a challenge — even in UIKit.
Now, let’s tackle Apple’s constraints together and explore how to customize the back button in SwiftUI.
Starting by hiding the back button
Our first task in this mission is to hide the default back button provided by Apple’s designers in the NavigationStack view. To do this, we’ll use the navigationBarBackButtonHidden view modifier:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
VStack {
NavigationLink("Go!") {
Text("Next Screen")
}
}
.navigationBarBackButtonHidden(true)
.padding()
}
}
}
And it works! In this example, we see a simple NavigationStack with a link that takes us to a new view. The new view appears without the back button. The navigationBarBackButtonHidden view modifier (yes, I can’t believe I have to type it again) not only hides the back button but also disables the swipe-to-back gesture we know and love. Wait, what?
Bringing back the swipe-to-back gesture
Do you know that feeling when Apple seems to know better than us what we want? Well, hiding the back button is probably another one of those situations. Since the back button is tightly integrated with the navigation system, Apple assumes that if we hide it, the swipe-to-back gesture should also be disabled because the navigation action is now considered irrelevant.
The bad news? There’s no way to bring this gesture back using SwiftUI alone.
The good news? We still have UIKit to save the day! By adding a simple extension, we can restore this feature:
import UIKit
extension UINavigationController: @retroactive UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
true
}
}
In this example, we extend UINavigationController and ensure the pop gesture is enabled if there’s more than one view controller in the navigation stack.
Let’s summarize what we’ve done so far:
- We hid the back button.
- We re-enabled the swipe-to-back gesture.
Now, we’re ready to add our custom back button.
Adding a custom back button
To add a custom back button, we can use the toolbar view modifier and add a new ToolbarItem:
import SwiftUI
struct ContentView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
NavigationStack {
VStack {
NavigationLink("Go!") {
Text("Next Screen")
}
}
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button(action:{
dismiss()
}) {
ZStack {
Circle()
.stroke(lineWidth: 2)
Image(systemName: "chevron.backward")
}
.frame(width:40, height:40)
}
}
}
.padding()
}
}
}
Here, the ToolbarItem is placed in topBarLeading, which is exactly where the back button normally appears. We also use the dismiss environment variable to access the dismiss action (which performs a pop action when the view is part of a stack).
The only issue now is that we have to hide the back button and add the custom back button in every view. Fortunately, we can save ourselves some trouble by creating a reusable view modifier.
Wrap everything using a view modifier
In this view modifier, we can hide the built-in back button and add our custom button in just one step:
struct ApplyToAllViews: ViewModifier {
@Environment(\.dismiss) private var dismiss
func body(content: Content) -> some View {
content
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button(action:{
dismiss()
}) {
ZStack {
Circle()
.stroke(lineWidth: 2)
Image(systemName: "chevron.backward")
}
.frame(width:40, height:40)
}
}
}
}
}
To use the view modifier, we simply apply it to our view:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
VStack {
NavigationLink("Go!") {
Text("Next Screen")
.modifier(ApplyToAllViews())
}
}
.padding()
}
.modifier(ApplyToAllViews())
}
}
Unfortunately, there isn’t a way to enforce this behavior across all views automatically. But hey, we still managed to outsmart Apple, didn’t we?
The full code is my GitHub repository:
Originally published on Medium.
Comments