iosuitableviewuipageviewcontroller

Access UIPageViewController gesture recognizer(s) to enable swipe to delete


I am trying to have swipe to delete on a UITableView work in conjunction with UIPageViewController's paging mechanism. I want to set the UIPageViewController's gesture recognizer so I can change it's delegate and implement:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    if let _ = touch.view as? UITableViewCell {
        return false
    }

    return true
}

My problem is that I can't find the UIPageViewController's gesture recognizer. I have tried looking in:

myPageViewController.gestureRecognizers
myPageViewController.view.gestureRecognizers

myPageContentViewController.gestureRecognizers
myPageContentViewController.view.gestureRecognizers

Where is it and/or is there a better solution to implementing a UITableView's swipe to delete functionality in a UIPageViewController?


Solution

  • The gestures are attached to its scrollView, and this one is not a public attribute. Anyway I use this extension to get the scrollView :

    extension UIPageViewController {
    
        public var scrollView: UIScrollView? {
            for view in self.view.subviews {
                if let scrollView = view as? UIScrollView {
                    return scrollView
                }
            }
            return nil
        }
    
    }
    

    Then you want its panGesture :

    pageController.scrollView?.panGestureRecognizer