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.
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")
}
}
}
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")
}
}
Quinn is right. but if You don't want a big area above:
add:
struct SecondView: View {
var body: some View {
Text("My View")
.navigationBarTitle("Second View", displayMode: .inline)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With