Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Navigation Multiple back button

Tags:

swiftui

When I push more than one view, multiple back buttons are visible in the navigation bar.

struct ContentView: View {
    var body: some View {
        NavigationView {
             NavigationLink(destination:SecView()) {
                   Text("Primo")
               }
        }
    }
}

struct SecView: View {
    var body: some View {
        NavigationView {
             NavigationLink(destination:TerView()) {
                   Text("Secondo")
               }
        }
    }
}

struct TerView: View {
    var body: some View {
        Text("Hello World!")
    }
}

I would like to have only one back button per view.

Here is a screenshot of the problem.

enter image description here

like image 424
Stefano Toppi Avatar asked Oct 20 '19 22:10

Stefano Toppi


1 Answers

There should only be a single NavigationView at the root of your navigation stack.

Remove the NavigationView block from SecView and you will then have a single navigation bar owned by ContentView.

like image 176
Gene Z. Ragan Avatar answered Sep 17 '22 11:09

Gene Z. Ragan