Search code examples
swiftuiswiftui-navigationlinkswiftui-navigationview

Why NavigationView in SwiftUI work like this?


I would like to make a navigation from Demo() to MainView(). After running on the Preview Provider, the Demo view is nested on the left hand side instead of showing an entire page. How to fix it?

Before clicking the icon:

enter image description here

After clicking the icon:

enter image description here

Here is my code:

struct Demo: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: MenuView()) {
                Text("Go")
            }
        }
    }
}

Solution

  • You can use

    .navigationViewStyle(.stack)

    
    struct Demo: View {
        var body: some View {
            NavigationView {
                NavigationLink(destination: MenuView()) {
                    Text("Go")
                }
            }
            .navigationViewStyle(.stack)
        }
    }