I'm hitting a wall. I have a UIScrollView with a child UIView (contentView) which contains the content and a button. The button is not detecting any taps. I've tested many methods, creating the button programmatically, using storyboard, and creating a UITapGestureRecognizer in the contentView... still nothing. From what I understand, UIScrollView does not delegate any touch events to it's children. How do I solve this?
            Asked
            
        
        
            Active
            
        
            Viewed 411 times
        
    1
            
            
        - 
                    Have ever try to put your `UIScrollView` inside a `UIView`? The hierarchy should be: `ViewController -> UIView -> UIScrollView -> UIView -> UIButton`. – Breek Jan 26 '16 at 17:49
 - 
                    @Breek If I'm not mistaken, a UIViewController has an embedded view by default, as when you're adding a UIScrollView programmatically: view.addSubview(scrollView)... Regardless, I programmatically added a parent UIView to my UIScrollView and still the contentView is not receiving tap events. – diskodave Jan 26 '16 at 18:08
 - 
                    Could you add some code about how you create your button programmatically so we can see the problem you have :) – Breek Jan 26 '16 at 18:43
 - 
                    Possible duplicate of [UIButton does not work when it in UIScrollView](http://stackoverflow.com/questions/16649639/uibutton-does-not-work-when-it-in-uiscrollview) – shpasta Jan 26 '16 at 19:09
 
1 Answers
1
            
            
        You can create the button programmatically then you add it as a subview for your contentView.
let button = UIButton()
button.frame = CGRectMake(contentView.center.x-20.0, contentView.center.y-20.0, 40.0, 40.0)
button.addTarget(self, action: "handleTap", forControlEvents: UIControlEvents.TouchUpInside)
button.setTitle("Title", forState: UIControlState.Normal)
contentView.addSubview(button)
On the action you can handle the tap
func handleTap() {
    //Do whatever you want
    print("Button tapped")
}
I tested it using a scrollView and a UIView inside the scrollView