[英]How can I hide the navigation back button in SwiftUI?
navigationBarBackButtonHidden(_ hidesBackButton: Bool) -> some View
但它仍然显示后退按钮,我想在点击时删除后退功能。
也许:
.navigationBarBackButtonHidden(true)
I tried placing .navigationBarBackButtonHidden(true)
in a few different places.我尝试将
.navigationBarBackButtonHidden(true)
放在几个不同的地方。 This is the behaviour I observed.这是我观察到的行为。
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!")
}
}
This is the solution, but it doesn't work on Xcode 11 beta 4:这是解决方案,但它不适用于 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)
}
}
There is also navigationBarHidden
which doesn't work on the iPhone, but it works perfectly on watchOS .还有
navigationBarHidden
,它在 iPhone 上不起作用,但它在watchOS上工作得很好。
I encountered a situation where I couldn't get the .navigationBarBackButtonHidden(true)
to work until I placed it on the View that I embedded within the NavigationLink
itself.我遇到了一种情况,在将
.navigationBarBackButtonHidden(true)
放在嵌入到NavigationLink
本身中的视图上之前,我无法使其工作。
NavigationLink(destination:MyView(stuff: aStuff, onDismiss: {})) {
HStack {
Text(aStuff.interestingText)
}
} // <- used to set it here, doesn't work for me
with:和:
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
}
}
Using via a navigationLink通过导航链接使用
NavigationLink(destination: SomePage().navigationBarBackButtonHidden(true), tag: 1, selection: $selection) {
//..
}
The .navigationBarBackButtonHidden(true)
will hide the back button. .navigationBarBackButtonHidden(true)
将隐藏后退按钮。
Perhaps your back button is on the left. 也许您的后退按钮在左侧。
Try adding this to your function: 尝试将其添加到您的函数中:
self.navigationItem.leftBarButtonItem = nil
Or right 还是对的
self.navigationItem.rightBarButtonItem = nil
Edit: 编辑:
Could also override viewWillDisappear() 也可以覆盖viewWillDisappear()
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationItem.hidesBackButton = true
print("backbutton hidden")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.