[英]SwiftUI double navigation bar
I am having trouble with navigation in SwiftUI.我在 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
.从
SecondView
中删除NavigationView
。
The NavigationLink
puts the second view inside the first views navigations view, so you do not need to put it inside a second one. NavigationLink
将第二个视图放在第一个视图导航视图中,因此您不需要将它放在第二个视图中。
You can still update the title of the view from SecondView
like so:您仍然可以像这样从
SecondView
更新视图的标题:
struct SecondView: View {
var body: some View {
Text("My View")
.navigationBarTitle("Second View")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.