2

Problem: How can I increase the range of the swipe gesture when popping out of a navigation view? Currently swipes are only registered on a small margin from the left of the device. I would like to make the swipe be registered on the entire screen like instagram you can swipe from anywhere.enter image description here

I have this code which enables swiping again after hiding the nav bar with .navigationBarHidden(true). I just cannot figure out how to increase the swiping range. Any thoughts? Thanks

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

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

Similar Solution: I have found this answer to the same question I can't figure out how to get it to work. There are no errors, I can't get any response unless I unhide the navbar Swipe to go back only works on edge of screen?

crow
  • 81
  • 6
  • To my point of view, full screen slide concept is only for sidemenu(Side drawer), but if you really want to implement something like you want right now then you can go with this UIPagerViewController :- https://spin.atomicobject.com/2015/12/23/swift-uipageviewcontroller-tutorial/ – Kishan Barmawala Feb 10 '21 at 09:42

1 Answers1

2

For a SwiftUI perspective, you can add swipe gesture as a modifier and add it to your SwiftUI View.

For a swipe gesture to be accessible on all the view, you can use .contentShape(Rectangle()).

Here, is the modifier for a swipe right gesture on a View:

import SwiftUI

struct GestureSwipeRight: ViewModifier {

  func body(content: Content) -> some View {
    content
      .contentShape(Rectangle())  // This is what would make gesture
      .gesture(                   // accessible on all the View.
        DragGesture(minimumDistance: 30, coordinateSpace: .local)
          .onEnded { value in
            if value.translation.width > .zero
                && value.translation.height > -30
                && value.translation.height < 30 {

              // Add your actions here when user swipe right.
            }
          }
      )
  }
}

You can then add this gesture to any view when you need it this way:

import SwiftUI

struct ContentView: View {

  var body: some View {
    VStack {
      // Your view body declaration
    }
    .modifier(GestureSwipeRight())
  }
}
Roland Lariotte
  • 2,606
  • 1
  • 16
  • 40
  • Thank you for sharing, but this is not a complete answer. With gestures, how to navigate back? – gaohomway Jan 04 '22 at 12:49
  • Just add the method in the commented part of the `GestureSwipeRight`, where it is said `Add your actions here when user swipe right`. – Roland Lariotte Jan 06 '22 at 07:52
  • I don’t know much about `GestureSwipeRight`, can you take the time to perfect this answer, thank you very much – gaohomway Jan 06 '22 at 09:28
  • Please, if you have a specific question, ask a new one on StackOverflow and add the link as a comment underneath this one. The community and I will try to answer your needs. – Roland Lariotte Jan 06 '22 at 13:58