Asked
Viewed
50k times
navigationBarBackButtonHidden(_ hidesBackButton: Bool) -> some View
But it still shows the back button and I want to remove the back function when clicked.
1
Maybe:
.navigationBarBackButtonHidden(true)
1
I tried placing .navigationBarBackButtonHidden(true)
in a few different places. 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!")
}
}
0
Using via a navigationLink
NavigationLink(destination: SomePage().navigationBarBackButtonHidden(true), tag: 1, selection: $selection) {
//..
}
The .navigationBarBackButtonHidden(true)
will hide the back button.
3
This is the solution, but it doesn’t work on 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.
1
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.
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
}
}
0
lang-swift