Reputation: 5
I've got a problem with SwiftUI. Basically this is my view graph:
View1 -> View2 -> View3 -> etc.
Now i want to show a back button in View2, but when user gets to view 3, it shouldn't be visible. I tried hiding and removing items, but still i can see back button on next views which will get me back to View1.
Upvotes: 0
Views: 1422
Reputation: 654
Without seeing your Code it's quite hard to tell where the issue is.
However the following Code is working for me
struct ContentView: View {
var body: some View {
NavigationView {
FirstView()
}
}
}
struct FirstView: View {
var body: some View {
NavigationLink(destination: SecondView()
) {
Text("Go to Second View")
}
.navigationBarTitle("FirstView", displayMode: .inline)
}
}
struct SecondView: View {
var body: some View {
NavigationLink(destination: ThirdView().navigationBarBackButtonHidden(true)
) {
Text("Go to Third View")
}
.navigationBarTitle("SecondView", displayMode: .inline)
}
}
struct ThirdView: View {
var body: some View {
Text("Third View without Back Button")
.navigationBarTitle("Third View", displayMode: .inline)
}
}
Upvotes: -1
Reputation: 5
As i was unable to do this the pure and nice way, i created a custom button, with dismiss action, which has content and the action works if only property isShowingButton is true, then on getting to next view i toggle it to false. It works, maybe not nicest way but works. If somehow someone knows a better way to solve it, i'd be grateful
Upvotes: 0
Reputation: 654
When navigating to View 3 you need to add .navigationBarBackButtonHidden(true) to the NagivationLink
struct ViewTwo: View {
var body: some View {
NavigationLink(destination: SecondView().navigationBarBackButtonHidden(true)
) {
Text("Go to View 3")
}
.navigationBarTitle("View 2", displayMode: .inline)
}
}
Upvotes: -1