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:
struct DetailView: View { var body: some View { Text("Detail screen") } }
struct ContentView: View { var body: some View { NavigationView { NavigationLink(destination: DetailView()) { Text("Go to detail") } } } }
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.