In UIKit, UINavigationItem has this useful property called backButtonDisplayMode that can be used to configure the behavior of the back button.
I was just wondering if SwiftUI has something similar? Alternatively, what is the best way to achieve this in SwiftUI? I would prefer to not have to use a custom back button, and really just want the back button to say "Back" instead of the previous view's navigation title.
CodePudding user response:
You have to do a custom button because SwiftUI is declarative, not imperative, but they are pretty simple.
struct CustomBackButton: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Text("Hello, World!")
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image(systemName: "chevron.left")
Text("Back")
}
}
}
}
}
}