0
votes

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.

3
Would you show your code?Asperi
@Asperi It's a pretty huge code , generally i have View1 with NavigationView and navigationView to View2, where i want to present a back button, which also has navigationView with navigationLink to View3 where i don't want back button anymoreMaciej Mikołajek

3 Answers

0
votes

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

-1
votes

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)
    }
}

Navigation without Back Button

-1
votes

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)

}

}