14

if I set a custom Back Button (which everyone wants, hiding the ugly text ;-) ) and using .navigationBarBackButtonHidden, the standard Swipe Back gesture on the navigation controller does not work. Is there a way to get this back and having a custom back button?

For Example:

NavigationView {
    NavigationLink(destination: DummyViewer())
     {
       Text("Go to next view"
    } 
 }
struct DummyViewer: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    var body: some View {
        Text("Hello, World!").navigationBarBackButtonHidden(true)
            .navigationBarItems(leading:
                Button(action: { self.presentationMode.wrappedValue.dismiss()}) {
                    Text("Custom go back")
                }
        )
    }
}

If I do so, I cannot swipe back to the previous view, seems the gesture is then disabled... How to get it back?

BR Steffen

sTOOs
  • 324
  • 3
  • 8
  • 1
    Does this answer your question? [Hide navigation bar without losing swipe back gesture in SwiftUI](https://stackoverflow.com/questions/59921239/hide-navigation-bar-without-losing-swipe-back-gesture-in-swiftui) – iSpain17 Feb 17 '20 at 19:52

4 Answers4

57

Nothing I found about creating a custom NavigationView worked but I found that by extending UINavigationController I was able to have a custom back button and the swipe back gesture.

extension UINavigationController: UIGestureRecognizerDelegate {
    override open func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
    }

    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    }
}
Nick Bellucci
  • 1,496
  • 10
  • 8
0

If it's still actual, here I answered, how to set custom back button and save swipe back gesture.

Hrabovskyi Oleksandr
  • 2,526
  • 2
  • 12
  • 32
0

I would like to integrate the answer given by Nick Bellucci to make the code also works in other circumstances, e.g. when the child view of the NavigationView is a ScrollView, or a View that is listening for Drag gestures.

extension UINavigationController: UIGestureRecognizerDelegate {
    override open func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
    }

    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    } 

    // To make it works also with ScrollView
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        true
    }
}
-1

You can set the title to an empty string. So back bar button title will be empty:

struct ContentView: View {

    var body: some View {

        NavigationView {

            NavigationLink(destination: Text("Here you are")) {

                Text("Next").navigationBarTitle("")
            }
        }
    }
}

You can set the title onAppear or onDisappear if you need to.

Mojtaba Hosseini
  • 71,072
  • 19
  • 226
  • 225
  • Hi, I added some more information. It is about the Back Button and the Swipe gesture which it causes - or not. – sTOOs Dec 08 '19 at 13:51