Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide the navigation back button in SwiftUI?

Tags:

swift

swiftui

navigationBarBackButtonHidden(_ hidesBackButton: Bool) -> some View

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

like image 696
Daniel Kua Avatar asked Jul 19 '19 11:07

Daniel Kua


People also ask

How do I hide the back button in navigation view in SwiftUI?

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

How do I hide the navigation bar in SwiftUI?

Use navigationBarHidden(_:) to hide the navigation bar. This modifier only takes effect when this view is inside of and visible within a NavigationView .

How do I hide the back button?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.


5 Answers

Maybe:

.navigationBarBackButtonHidden(true)
like image 90
Joannes Avatar answered Oct 04 '22 18:10

Joannes


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!")
    }
}
like image 40
Karin Avatar answered Oct 04 '22 18:10

Karin


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.

like image 42
JM_mobile_dev Avatar answered Oct 04 '22 19:10

JM_mobile_dev


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
    }
}
like image 25
Kheldar Avatar answered Oct 04 '22 19:10

Kheldar


Using via a navigationLink

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

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

like image 26
Shahzar Ali Avatar answered Oct 04 '22 18:10

Shahzar Ali