I have a subview with a UIButton added to a UIScrollView.
The button is working perfectly as long as the user isn’t scrolling.
If the UIScrollView is still scrolling when the user clicks on the button, it just stops the scrolling instead(like if a row had been clicked).
Anyone know how to fix this?
First, make sure this really is the behavior you want – iOS users are used to scrolling views and touching to stop them with a tap without triggering button presses. Non-standard behavior (even when you think its better then the standard behavior) can be confusing to users used to how things work in other iOS apps – it can violate their mental model. Ok, note of caution over.
So how do you fix this?
UIScrollViewdelays sending touch events until it knows if those touches are scroll events. You problem is a user tapping is a scroll event when theUIScrollViewis moving. Two possible solutions:Stop the
UIScrollViewfrom delaying any touch events it gets. You can set any scroll viewsdelaysContentTouchestoNO, which will stop the delaying action and should allow your buttons to be tapped while scrolling. You can read about it in theUIScrollViewclass documentation. You will also want to read up oncanCancelContentTouchesthere as well.Subclass the
UIScrollViewto add your own logic about where touch events should go. Here is a blog post that discusses how to do this.