How to make a navigation with SwiftUi

Asked on 08/10/2024

1 search

To create navigation in SwiftUI, you can use the NavigationView and NavigationLink components. These components allow you to build a navigation hierarchy in your app. Here's a basic example:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: DetailView()) {
                    Text("Go to Detail View")
                }
            }
            .navigationTitle("Main View")
        }
    }
}

struct DetailView: View {
    var body: some View {
        Text("This is the Detail View")
            .navigationTitle("Detail View")
    }
}

In this example, NavigationView is used to wrap the content that should be navigable. NavigationLink is used to define a link to another view, in this case, DetailView. When the user taps on the text "Go to Detail View," they are navigated to the DetailView.

For more advanced navigation, you can explore the navigationSplitView for creating split views or customizable multicolumn tables, as mentioned in the SwiftUI essentials session.