navigationBarBackButtonHidden(_ hidesBackButton: Bool) -> some View
但它仍然显示返回按钮,我想在单击时删除返回功能。
.navigationBarBackButtonHidden(true)
我尝试将.navigationBarBackButtonHidden(true)
放在几个不同的位置。这是我观察到的行为。
struct PageOne: View {
var body: some View {
NavigationView {
VStack{
NavigationLink(destination: PageTwo()){
Text("Go to Page Two")
}
}
}
}
}
// Hide from page 2 -> page 1
struct PageTwo: View {
var body: some View {
VStack{
NavigationLink(destination: PageThree()){
Text("Go to Page Three")
}.navigationBarBackButtonHidden(true)
}
}
}
// Hide from page 3 -> page 2 (Same behaviour as Kheldar's answer above)
struct PageTwo: View {
var body: some View {
VStack{
NavigationLink(destination: PageThree().navigationBarBackButtonHidden(true)){
Text("Go to Page Three")
}
}
}
}
struct PageThree: View {
var body: some View {
Text("Hello!")
}
}
通过导航链接使用
NavigationLink(destination: SomePage().navigationBarBackButtonHidden(true), tag: 1, selection: $selection) {
//..
}
.navigationBarBackButtonHidden(true)
会隐藏返回按钮。
这是解决方法,但它在Xcode 11 beta 4上不起作用:
struct LiveView: View {
var body: some View {
NavigationView {
NavigationLink(destination: ButtonView()) {
Text("Next screen")
}
}
}
}
struct ButtonView: View {
@State var navigationBarBackButtonHidden = true
var body: some View {
Button("Show back") {
self.navigationBarBackButtonHidden = false
}.navigationBarBackButtonHidden(navigationBarBackButtonHidden)
}
}
还有navigationBarHidden
,在iPhone上不起作用,但在watchOS上可以完美运行。
.navigationBarBackButtonHidden(true)
代码,也不能起作用,直到我将其放置在嵌入在NavigationLink
中的视图上。请注意保留HTML标签。NavigationLink(destination:MyView(stuff: aStuff, onDismiss: {})) {
HStack {
Text(aStuff.interestingText)
}
} // <- used to set it here, doesn't work for me
使用:
struct MyView: View {
var aStuff: Stuff
var onDismiss: () -> Void
var body: some View {
VStack(alignment: .leading) {
Button(action: self.onDismiss) {
Image(systemName: "chevron.left.circle")
}
CoolAnimatedStuffDisplayer(stuff: aStuff)
}
.navigationBarBackButtonHidden(true) // <--- works here
}
}