struct MyNavigation<Content>: View where Content: View {
@ViewBuilder var content: () -> Content
@Environment(.dismiss) private var dismiss
var body: some View {
NavigationStack(root: content)
.navigationBarBackButtonHidden()
// .toolbarRole(.editor)
.navigationBarItems(leading: CustomBackButton(dismiss: self.dismiss))
}
}
struct CustomBackButton: View {
let dismiss: DismissAction
var body: some View {
Button {
dismiss()
} label: {
Image("icon_back")
}
}
}
I want to change the native back button image to my back icon, not to create a custom back button so that I can use all native functionality. like swipe back gesture not extra code needs to be written in swiftui ios 16 minimum.
I tried
.navigationBarBackButtonHidden()
.toolbarRole(.editor)
This will remove the back button text but not the default back button icon. I need to change the icon.
There is a well written article that you can find here.
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.
For a quick answer, call this as early as possible. For more customization options, feel free to checkout at the article.
ContentView()
.onAppear {
UINavigationBar.appearance().backIndicatorImage = UIImage(systemName: "arrow.backward")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(systemName: "arrow.backward")
}