Skip to content
Join us for Free for Full Access to site Content

NavigationLink – SwiftUI

How to create a NavigationLink in a NavigationView in SwiftUI

We have a ContentView. Let’s say we want to present a DetailView. Let’s do it using a NavigationLink which lives inside a NavigationView. A NavigationView is a view for presenting a stack of views representing a visible path in a navigation hierarchy. To achieve this use this code for a ContentView:

struct ContentView: View {
    
    var body: some View {
        
        NavigationView {
            NavigationLink(destination: DetailView()) {
                Text("Tap Me")
            }
        }
        
    }
    
}

struct DetailView: View {
    
    var body: some View {
        
        Text("Hello, I'm a Detail View")
        
    }
    
}

How to show a NavigationBar title when using a NavigationView with a NavigationLink

To show a navigation Title, you can use a .navigationBarTitle(_ title: Text) modifier.

var body: some View {
        
        NavigationView {
            NavigationLink(destination: DetailView()) {
                Text("Tap Me")
            }.navigationBarTitle(Text("ContentView"))
        }
        
    }

Check more about NavigationView here

Learn more about SwiftUI using SwiftUI Tutorials by the AppMakers

If anything will change in the future, it is OK, you can always check the official documentation.

NavigationLink Documentation

Leave a comment to Submit the NEW and USEFUL code related to the topic of this post.
Submit a brief and useful code only.

Back To Top
Search

Get Latest App Development News, Tips and Tutorials

* indicates required


Send this to a friend