struct DevTechieNavigationLinkExample: View { var body: some View { NavigationView { VStack { Text("Hello, DevTechie") .font(.largeTitle) NavigationLink(destination: Text("You reached here via NaviagtionLink")) { HStack { Image(systemName: "computermouse") Text("Click Me!") } } .padding() } } } }
Note that we are using a Text view as destination for our example but SwiftUI presents entire view along with back button to complete the experience.
struct DevTechieNavigationLinkExample: View { var body: some View { NavigationView { VStack { Text("Hello, DevTechie") .font(.largeTitle) NavigationLink { VStack { Text("Hello DevTechie!") .font(.largeTitle) .foregroundColor(.white) .padding() .background(Color.orange, in: RoundedRectangle(cornerRadius: 20)) Text("Welcome!") .font(.title) Text("Let's learn and grow together!") } } label: { HStack { Image(systemName: "computermouse") Text("Click Me!") } } .padding() } } } }
struct DevTechieNavigationLinkExample: View { var body: some View { NavigationView { VStack { Text("Hello, DevTechie") .font(.largeTitle) NavigationLink(destination: NavDestinationView(), label: { HStack { Image(systemName: "computermouse") Text("Click Me!") } }) .padding() } } } }
struct DevTechieNavigationLinkExample: View { var body: some View { NavigationView { VStack { Text("Hello, DevTechie") .font(.largeTitle) NavigationLink(destination: NavDestinationView(), label: { HStack { Image(systemName: "computermouse") Text("Click Me!") } }) .padding() } .navigationTitle("DevTechie.com") } } }struct NavDestinationView: View { var body: some View { VStack { Text("Hello DevTechie!") .font(.largeTitle) .foregroundColor(.white) .padding() .background(Color.orange, in: RoundedRectangle(cornerRadius: 20)) Text("Welcome!") .font(.title) Text("Let's learn and grow together!") } .navigationTitle("NavDestination View") } }
Notice that the NavDestinationView’s body doesn’t contain NavigationView.
struct DevTechieNavigationLinkExample: View { @State private var triggerNavigation = false var body: some View { NavigationView { VStack { Text("Hello, DevTechie") .font(.largeTitle) NavigationLink("Click Me!", destination: NavDestinationView(), isActive: $triggerNavigation) .padding() Button("Click me to navigate") { triggerNavigation.toggle() } .buttonStyle(.borderedProminent) } .navigationTitle("DevTechie.com") } } }
Note: Both navigation link and button will trigger the navigation push command. triggerNavigation parameter resets to false value when back button is tapped on the detail view.
struct DevTechieNavigationLinkExample: View { @State private var selected: Int? = nil var body: some View { NavigationView { VStack { Text("Hello, DevTechie") .font(.largeTitle) VStack { NavigationLink("Click View 1!", destination: NavDestinationView(viewName: "View one"), tag: 1, selection: $selected) NavigationLink("Click View 2!", destination: NavDestinationView(viewName: "View two"), tag: 2, selection: $selected) NavigationLink("Click View 3!", destination: NavDestinationView(viewName: "View three"), tag: 3, selection: $selected) } .padding() HStack { Button("View 1") { selected = 1 } Button("View 2") { selected = 2 } Button("View 3") { selected = 3 } } } .navigationTitle("DevTechie.com") } } }struct NavDestinationView: View { var viewName: String var body: some View { VStack { Text(viewName) Text("Hello DevTechie!") .font(.largeTitle) .foregroundColor(.white) .padding() .background(Color.orange, in: RoundedRectangle(cornerRadius: 20)) Text("Welcome to the \(viewName)!") .font(.title) Text("Let's learn and grow together!") } .navigationTitle("NavDestination View") } }
struct DevTechieNavigationLinkExample: View { var body: some View { NavigationView { VStack { Text("Hello, DevTechie") .font(.largeTitle) NavigationLink(destination: NavDestinationView(viewName: "Detail View"), label: { HStack { Image(systemName: "computermouse") Text("Click Me!") } }) .buttonStyle(.borderedProminent) .padding() } } } }
struct NavDestinationView: View { @Environment(\.presentationMode) var presentationMode var viewName: String var body: some View { VStack { Text(viewName) Text("Hello DevTechie!") .font(.largeTitle) .foregroundColor(.white) .padding() .background(Color.orange, in: RoundedRectangle(cornerRadius: 20)) Text("Welcome to the \(viewName)!") .font(.title) Text("Let's learn and grow together!") Button("Dismiss") { presentationMode.wrappedValue.dismiss() } .buttonStyle(.borderedProminent) .tint(.red) } .navigationBarHidden(true) .navigationTitle("NavDestination View") } }
struct NavDestinationView: View { @Environment(\.dismiss) var dismiss var viewName: String var body: some View { VStack { Text(viewName) Text("Hello DevTechie!") .font(.largeTitle) .foregroundColor(.white) .padding() .background(Color.orange, in: RoundedRectangle(cornerRadius: 20)) Text("Welcome to the \(viewName)!") .font(.title) Text("Let's learn and grow together!") Button("Dismiss") { dismiss() } .buttonStyle(.borderedProminent) .tint(.red) } .navigationBarHidden(true) .navigationTitle("NavDestination View") } }
struct DevTechieNavigationLinkExample: View { var body: some View { NavigationStack { VStack { Text("DevTechie") .font(.largeTitle) NavigationLink("Next page", value: "New Detail View") } .navigationDestination(for: String.self) { val in NavDestinationView(viewName: val) } } } }
struct DevTechieCourse: Identifiable, Hashable { let id = UUID() let name: String }extension DevTechieCourse { static var exampleData: [DevTechieCourse] { return [ .init(name: "Mastering SwiftUI"), .init(name: "Build Disney Plus Clone in SwiftUI"), .init(name: "Build Video Player App in SwiftUI"), .init(name: "Build Drawing App in SwiftUI") ] } }
struct DevTechieNavigationLinkExample: View { var body: some View { NavigationStack { List(DevTechieCourse.exampleData) { course in NavigationLink(course.name, value: course) } .listStyle(.plain) .navigationTitle("DevTechie Courses") .navigationDestination(for: DevTechieCourse.self) { course in Text(course.name) } } } }
With that we have reached the end of this article. Thank you once again for reading. If you liked this, don’t forget to 👏 and follow 😍. Also subscribe to our weekly newsletter at https://www.devtechie.com