user1366265
user1366265

Reputation: 1426

SwiftUI double navigation bar

I am having trouble with navigation in SwiftUI. I have a button on a navigation bar, if clicked it pushes a new navigation view with list of items. When one of those items is tapped, it pushes a detail view.

But I am ending up with something like this. enter image description here

Below is the code

struct FirstView: View {

   var body: some View {
       NavigationView {
           List {
            ...
           }
           .navigationBarTitle(Text("First View"))
           .navigationBarItems(trailing: MyButton())
       }
    }
}

struct MyButton: View {
    var body: some View {
        NavigationLink("SecondView", destination: SecondView())
    }
}

struct SecondView: View {
   var body: some View {
       NavigationView {
           Text("My View")
       }
   }
}

Upvotes: 20

Views: 11618

Answers (2)

ingconti
ingconti

Reputation: 11636

Quinn is right. but if You don't want a big area above:

enter image description here

enter image description here

add:

struct SecondView: View {
   var body: some View {
       Text("My View")
        .navigationBarTitle("Second View", displayMode: .inline)
   }
}

Upvotes: 3

Quinn
Quinn

Reputation: 9404

Remove the NavigationView from SecondView.

The NavigationLink puts the second view inside the first views navigations view, so you do not need to put it inside a second one.

You can still update the title of the view from SecondView like so:

struct SecondView: View {
   var body: some View {
       Text("My View")
       .navigationBarTitle("Second View")
   }
}

Upvotes: 46

Related Questions