63
navigationBarBackButtonHidden(_ hidesBackButton: Bool) -> some View

But it still shows the back button and I want to remove the back function when clicked.

pawello2222
  • 46,897
  • 22
  • 145
  • 209
Daniel Kua
  • 763
  • 1
  • 5
  • 3
  • Welcome to SO! Could you please show more code? (Maybe even fort it as code?) It would be helpful for us to duplicate your issue. –  Jul 19 '19 at 12:01

5 Answers5

80

Maybe:

.navigationBarBackButtonHidden(true)
Joannes
  • 2,569
  • 3
  • 17
  • 39
23

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!")
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Karin
  • 233
  • 2
  • 7
18

Using via a navigationLink

NavigationLink(destination: SomePage().navigationBarBackButtonHidden(true), tag: 1, selection: $selection) {
    //..
}

The .navigationBarBackButtonHidden(true) will hide the back button.

shb
  • 5,957
  • 2
  • 15
  • 32
Shahzar Ali
  • 181
  • 1
  • 2
17

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
13

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
    }
}
Rohit Makwana
  • 4,337
  • 1
  • 21
  • 29
Kheldar
  • 5,361
  • 3
  • 34
  • 63