Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To add a custom back button in SwiftUI for iOS13, you can use the NavigationLink and NavigationBarButton components. Here is an example:

  1. Create a new SwiftUI view for your destination screen.
struct DetailView: View { var body: some View { Text("Detail screen") } } 
  1. In your main screen, add a NavigationLink that will link to the destination screen.
struct ContentView: View { var body: some View { NavigationView { NavigationLink(destination: DetailView()) { Text("Go to detail") } } } } 
  1. Now, to add a custom back button to the destination screen, you can use the NavigationBarButton component.
struct DetailView: View { @Environment(\.presentationMode) var presentationMode var body: some View { VStack { Text("Detail screen") Button(action: { self.presentationMode.wrappedValue.dismiss() }) { Text("Back") } } .navigationBarTitle("Detail") .navigationBarItems(trailing: Button(action: { // Do something }) { Image(systemName: "person.crop.circle") } ) } } 

The presentationMode environment variable allows access to the current presentation mode for the view. Using the wrappedValue property, we can dismiss the current screen by calling the dismiss() method.

The navigationBarItems modifier allows us to add custom navigation buttons to the navigation bar. In this example, we're adding a trailing button with an image.