UIButton inside UIScrollView doesn't fire on tap

I found this stuff:

But the scrollview will block the touches from the button unless you set userInteractionEnabled and exclusiveTouch properties to NO on the scrollview. But that would defeat the purpose of having a scrollview inside a button I think.

(See Can we put a UIButton in a UIScrollView and vice versa in iPhone)

But how to do it?

Say you have a UIScrollView *sv ... then

sv.userInteractionEnabled = YES;
sv.exclusiveTouch = YES;

I would also add

sv.canCancelContentTouches = YES;
sv.delaysContentTouches = YES;

You must set content size of your view. That must be greater than or equal to the content size of scrollView.

Because default size of your view is 320 x 480 (3.5" retina) and 320 x 568 (4" retina). So increasing height of view as:

self.view.frame=CGRectMake(0, 0, 320, 700);

Then adding this as the subview of your scrollView will get you to the solution.


OLD ANSWER: All I had to do in iOS 7 was set

[_scrollView setDelaysContentTouches:NO];

UPDATE: After doing a little more research it appears that if your issue is that the UIButton click seems to only be called sometimes, then that is actually probably the desired behavior inside a UIScrollView. A UIScrollView uses the delaysContentTouches property to automatically determine if the user was trying to scroll or trying to press a button inside the scroll view. I would assume it is best to not alter this behavior to default to NO since doing so will result in an inability to scroll if the user's finger is over a button.

Tags:

Iphone