My structure of views:
UITableView
UITableViewCell
UIScrollView
CustomView
UIButton
The problem is UIButton doesn't work when I touch it. I create it with code:
btn = [[UIButton alloc] init];
[btn setImage:image forState:UIControlStateNormal];
[btn addTarget:self action:@selector(tileTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
btn.layer.zPosition = 1;
[self addSubview:btn];
I've also add this:
scroll.delaysContentTouches = NO;
scroll.canCancelContentTouches = NO;
But my button doesn't work anyway. I can't cancelContentTouches or set delay for UITableView because if I set it, UITableView stop scrolling verticaly.
Thanks for any help!
Create a subclass of UIScrollview with
If you are not getting that message, set:
The problem is because of the way
touchesShouldCancelInContentView
behaves by default, it returns NO only if the subview is an UIControl, but your button is hidden inside the CustomView which isn't.I guess you need to set the button frame. By default if you use
btn = [[UIButton alloc] init];
method to initialize button it will beUIButtonTypeCustom
. Also set a background color to it for debugging purpose to note down where the button is actually placed. Now for your case you need to set the frame for that button likebtn.frame = CGRectMake(0,0,100,100);
For the inner UIScrollView implement this delegate method.
This may work.
None of the answers worked for me, because my problem was that the button was not visible at layout time or something...
FIX:
1) move the button out of the
scrollView
completely (to be subview of VC's mainview
).2) select the
button
and the element from yourcontentV
you want your button located (in my case: after the last element in conventV)3) add the necessary constraints (in my case: to be bottom aligned)
4) Add the rest of the constraints for the
button
.5) Enjoy.
Add all subviews from CustomView to ScrollView. And hide the CustomView. Make sure CustomView(ContainerView of scrollView) should also be a subView of scrollView.
Came across the same scenario. UIScrollView in separate class and adding button through a custom view. 'NSNotificationCenter' helped me solving the issue. Sample code:
In the class containing Scroll View:
Enable userInteraction for scroll view, custom view and button.
Link from Capt.Hook helped me. I used UIGestureRecognizer instead of UIButtons.
Allow UIScrollView and its subviews to both respond to a touch Use it, if you have the similar problem.