Ask Your Question

How to add a custom button in SwiftUI for iOS13 that enables going back between screens?

asked 2022-02-07 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-10-10 03:00:00 +0000

djk gravatar image

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: "") } ) } } 

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2022-02-07 11:00:00 +0000

Seen: 26 times

Last updated: Oct 10 '22