Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI double navigation bar

Tags:

swift

swiftui

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")
       }
   }
}
like image 881
user1366265 Avatar asked Sep 23 '19 14:09

user1366265


2 Answers

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")
   }
}
like image 187
Quinn Avatar answered Sep 21 '22 06:09

Quinn


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)
   }
}
like image 39
ingconti Avatar answered Sep 23 '22 06:09

ingconti