Updated for Xcode 12.5
If you have a navigation view and you want to push a new view onto SwiftUI’s navigation stack, you should use NavigationLink
. This takes a destination as its first parameter and what to show inside the button as its second parameter (or as a trailing closure), and takes care of pushing the new view on the stack for us along with animation.
For example, this creates a simple SecondView
struct, then presents it from a NavigationView
:
struct SecondView: View {
var body: some View {
Text("This is the detail view")
}
}
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: SecondView()) {
Text("Show Detail View")
}
.navigationTitle("Navigation")
}
}
}
}
SPONSORED ViRE offers discoverable way of working with regex. It provides really readable regex experience, code complete & cheat sheet, unit tests, powerful replace system, step-by-step search & replace, regex visual scheme, regex history & playground. ViRE is available on Mac & iPad.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.