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.
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
Asked: 2022-02-07 11:00:00 +0000
Seen: 15 times
Last updated: Oct 10 '22
How can the update of properties from both ParentViewModel and ChildViewModel in SwiftUI be solved?
Can SwiftUI be utilized to restrict the touch region for a drag gesture?
How can the color of a selected element (Picker) be modified in SwiftUI for WatchOS?
How to make a calendar layout that is horizontal using Swiftui?
What is the process to add a radio button with a check mark on a TableView cell using Swift?
How to limit UITextField input to only accept numerical values?