

Custom Back button in SwiftUI
source link: https://sarunw.com/posts/custom-back-button-in-swiftui/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

When talking about a custom Back button in a navigation view, it usually falls into two categories.
- Custom appearance.
- Custom action.
If you want to customize a back button action, you can read it in Custom Back button Action in SwiftUI.
In this article, we will focus on a custom Back button appearance.
Custom Back button in SwiftUI
How we customize an appearance of a back button varies based on the area we want the customization to take effect.
I categorize this into two groups.
You can easily support sarunw.com by checking out this sponsor.
Sponsor sarunw.com and reach thousands of iOS developers.
How to change a back button across the app
At the moment (iOS 16), SwiftUI has no native way to change the appearance of the back button.
Your best bet is to fall back to UIKit.
I wrote a detailed article about this in How to change a back button image.
Here is a snippet of how to do it.
UINavigationBar.appearance().backIndicatorImage = UIImage(systemName: "arrow.backward")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(systemName: "arrow.backward")
You call this at the earliest stage possible. It can be in the AppDelegate
or onAppear
of your root view.
@main
struct custom_back_buttonApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear {
UINavigationBar.appearance().backIndicatorImage = UIImage(systemName: "arrow.backward")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(systemName: "arrow.backward")
}
}
}
}
Here is the result. We change the back button image to UIImage(systemName: "arrow.backward")
.

A custom back button image.
You can easily support sarunw.com by checking out this sponsor.
Sponsor sarunw.com and reach thousands of iOS developers.
How to change a back button on a specific view
If you want to change the back button appearance of a specific view, we can use the same way we did in Custom Back button Action in SwiftUI.
That is to opt out of the default back button and recreate it.
Remove the default Back button
To remove the default back button, you apply .navigationBarBackButtonHidden(true)
to the view that you want to hide the back button.
In the following example, when the DetailView
get pushed to a navigation stack. It won't have a back button.
struct DetailView: View {
var body: some View {
Text("Detail View")
.navigationTitle("Detail Title")
.navigationBarBackButtonHidden(true)
}
}
Once you hide the default back button, all the functionality is gone.
- Tapping on your custom back button won't pop the view out of a navigation stack.
- Swiping from the leading edge of the screen is also disabled.
So, you need to implement the dismissal logic manually in your new button.
Recreate a Back button
A back button is just a button. We need to create a normal button and position it in the same place.
We can do that with a help of toolbar(content:)
and ToolbarItem(placement: .navigationBarLeading)
.
struct DetailView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
Text("Detail View")
.navigationTitle("Detail Title")
.navigationBarBackButtonHidden(true)
// 1
.toolbar {
// 2
ToolbarItem(placement: .navigationBarLeading) {
Button {
// 3
dismiss()
} label: {
// 4
HStack {
Image(systemName: "arrow.backward")
Text("Custom Back")
}
}
}
}
}
}
1 We use toolbar(content:)
to add a new toolbar item.
2 We position this to .navigationBarLeading
, the same position as the default back button.
3 We have to manually pop dimiss the view here.
4 This is where we customize the appearance of a back button.
Here is the result.

A custom back button appearance.
Recommend
-
34
In SwiftUI a button is a control that performs a action when triggered. In this tutorial a button can be clicked to increment a value.SwiftUI requires Xcode 11 and MacOS Catalina, for which the Betas can be
-
10
Posted 1 month ago2021-01-27T17:30:00+01:00 by Peter Steinberger Updated 1 month ago2021-02-03T16:12:59+01:00
-
22
The many faces of button in SwiftUI 30 Jun 2021Button is one of the crucial components of any app. We use buttons to provide actions in the user interface of the app. SwiftUI 3 released a bunch of new view modifiers that allow us t...
-
12
How to make a custom button style supports leading dot syntax in SwiftUI Table of ContentsYou can easily support sarunw.com by checking out this sponsor.
-
7
开发 SwiftUI Bug – PlainButtonStyle Button 触发次数异常 ...
-
7
Home iOS & Swift Tutorials Sw...
-
6
How to make SwiftUI button with buttonStyle expand to full width Table of Contents ...
-
5
In SwiftUI, we can add a button to a navigation bar by putting them in toolbar() modifier.
-
51
How to remove Back button title in SwiftUI 21 Feb 2023 ⋅ 2 min read ⋅ SwiftUI...
-
12
Custom back button not work in fiori launchpad
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK