UIButton inside UIScrollView doesn't fire on tap. Please help resolve it.
// Add a button
UIButton *btn1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn1.frame = CGRectMake(0, 0, 26, 18);
btn1.bounds = CGRectMake(0, 0, 30.0, 30.0);
[btn1 addTarget:self action:@selector(buttonClick1:)
forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:btn1];
- (void)buttonClick1:(id)sender {
int dd = 4;
}
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.
My structure is:
-View
--ScrollView
---ContainerView
and I am using snapkit. My issue was that my ContainerView actually had a height of 0. I was able to notice this by setting ClipsToBounds = true
on the ContainerView (nothing rendered after doing this as bounds had a height of 0).
Any view out of the parent view's bounds will not receive the touch up inside events.
My fix was to set the ContainerView left right and bottom anchors equal to the View and the top equal to the ScrollView (my ScrollView's top is offset from my root View's).
I was facing this same problem, what i did was instead of adding the button to the container view, i've added it to the scroll view on top of the container view:
scrollView.insertSubview(buttonA, aboveSubview: containerView)
My structure was:
-View
--ScrollView
---ContainerView
I hope this could be of help to any one.
This works for me in iOS 10
scrollView.delaysContentTouches = false
From Apple:
A Boolean value that determines whether the scroll view delays the handling of touch-down gestures.
https://developer.apple.com/documentation/uikit/uiscrollview/1619398-delayscontenttouches
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With