我有两个SwiftUI视图,它们是我与NavigationLink链接的独立文件:
View1:
struct ViewOne: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: ViewTwo()
.navigationBarBackButtonHidden(true)
) {
Text("Go to second view")
}
.navigationBarTitle(Text("First View"), displayMode: .inline)
}}}}
View2:
struct ViewTwo: View {
var body: some View {
NavigationView {
VStack {
Text("Hello world")
}
.navigationBarTitle(Text("Second View"), displayMode: .inline)
}}}
问题:在View2上,我不需要.navigationBarBackButton,因此我将其设置为在ViewOne中隐藏,但由于我将其设置为隐藏,因此它与ViewTwo上的现有NavigationBar混淆并显示多个NavigationBars
如何在不破坏View2上现有的NavigationBar的情况下禁用navigationBarBackButton?
答案 0 :(得分:0)
NavigationView
在一个视图层次结构中应该仅为一个,因此只需在ViewTwo
中将其删除:
struct ViewTwo: View {
var body: some View {
VStack {
Text("Hello world")
}
.navigationBarTitle(Text("Second View"), displayMode: .inline)
}
}