如何在SWIFT UI中隐藏导航后退按钮?

时间:2019-07-19 11:56:13

标签: swift swiftui

navigationBarBackButtonHidden(_ hidesBackButton: Bool) -> some View

但是它仍然显示后退按钮,我想在单击时删除后退功能。

6 个答案:

答案 0 :(得分:12)

我尝试将.navigationBarBackButtonHidden(true)放在几个不同的地方。这是我观察到的行为。希望对您有帮助!

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

答案 1 :(得分:3)

也许:

.navigationBarBackButtonHidden(true)

答案 2 :(得分:0)

这是解决方案,但不适用于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)
    }
}

还有navigationBarHidden不能在iPhone上运行,但可以在watchOS上完美运行。

答案 3 :(得分:0)

我遇到了这样一种情况,直到将.navigationBarBackButtonHidden(true)放在我嵌入到NavigationLink本身中的View上之前,它才能起作用。

NavigationLink(destination:MyView(stuff: aStuff, onDismiss: {})) {
            HStack {
                Text(aStuff.interestingText)
            }
        } // <- used to set it here, doesn't work for me

具有:

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
}

}

答案 4 :(得分:0)

通过导航链接使用
NavigationLink(destination: "SomePage").navigationBarBackButtonHidden(true), tag: 1, selection:$selection)

.navigationBarBackButtonHidden(true) 将隐藏后退按钮。

答案 5 :(得分:-1)

也许您的后退按钮在左侧。

尝试将其添加到您的函数中:

   self.navigationItem.leftBarButtonItem = nil 

或正确

   self.navigationItem.rightBarButtonItem = nil 

编辑:

还可以覆盖viewWillDisappear()

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationItem.hidesBackButton = true
    print("backbutton hidden")
}