I\'m trying to make a custom back button for the NavigationBar
where I can put whatever text I like. My primary use case is that some of my views have no title, and
If you're just trying to have custom text, then no need to bother with UIKit. Here's how you can make a custom back button in SwiftUI:
struct ViewWithCustomBackButton: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
HStack {
...
}
.navigationBarTitle(Text("Your View"), displayMode: .inline)
// Hide the system back button
.navigationBarBackButtonHidden(true)
// Add your custom back button here
.navigationBarItems(leading:
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image(systemName: "arrow.left.circle")
Text("Go Back")
}
})
}
}
You can also check out the answers here for more info: Custom back button for NavigationView's navigation bar in SwiftUI