-1

I have a few UIButtons on my UIPageViewController for turning the page.

If I try to switch pages by swiping when my initial touch lands on one of the buttons, the button blocks the scrollview from receiving the gesture, essentially preventing the page from scrolling.

Is there anyway to allow the buttons to be tapped but also allow the scrollview to be scrolled behind it if its a swipe?

Thank you!

user6520705
  • 705
  • 3
  • 11
  • 38

2 Answers2

0

How about using view or image then set them with tap gesture recognizer?

Mg Dana
  • 67
  • 1
  • 7
0

This may work for you...

  • Add the button as a subview of the scroll view
  • set the scroll view's class to PassTouchScrollView
  • constrain the top of the button to the top of the scroll view, with a constant to place it vertically where you want it
  • save the original constant, and update it when the scroll view scrolls to keep the button at its original y-position

Here is an example. Setup your scroll view and its content subviews as you normally would. Add your button as a subview, and constrain it to the top of the scroll view (not to its nearest neighbor). Set the scroll view's class to PassTouchScrollView and connect it via @IBOutlet. Connect the top-constraint via @IBOutlet. Finally, connect the button's touchUpInside to the @IBAction.

class PassTouchScrollView: UIScrollView {

    override func touchesShouldCancel(in view: UIView) -> Bool {
        if type(of: view) == UIButton.self {
            return true
        }

        return super.touchesShouldCancel(in: view)

    }

}

class PassThroughTestViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet var theScrollView: PassTouchScrollView!

    // constraint from top of button to top of scroll view
    @IBOutlet var theButtonTopConstraint: NSLayoutConstraint!

    var origTop: CGFloat = CGFloat(0)

    override func viewDidLoad() {
        super.viewDidLoad()

        theScrollView.delegate = self

        // save the original top constraint constant
        origTop = theButtonTopConstraint.constant
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // when scrolling, update the top constraint to keep the button in-place
        theButtonTopConstraint.constant = origTop + scrollView.contentOffset.y
    }

    @IBAction func didTap(_ sender: Any) {
        print("tapped")
    }

}
DonMag
  • 69,424
  • 5
  • 50
  • 86