Button not working in UIScrollView

I have button inside a view inside a scroll view. The button does not work.


How can I get the button to work?


I've consulted some answer elsewhere, but most are old and don't have swift examples.


Any help greately appreciated. Working with xcode 7.1 and swift 2.

Replies

What does 'not work' mean, exactly?


What did you expect....what actually happened - how do those two differ? Be specific.

The most common reasons for a button to be completely unresponsive to touches are 1) if the button or any of its superviews have userInteractionEnabled set to NO, or 2) if one of the superviews has an incorrectly laid out frame such that the button is drawn outside the bounds. Since clipsToBounds is NO by default, subviews can be drawn outside the bounds but won't respond to touches.


Or, if it is responding to touches (highlights / unhighlights when touched, but nothing else happens) then you don't have your action hooked up properly.

I was aware of both reasons.


I have "User Interaction Enabled" checked on the button and for the Scroll View and I have "Delays Content Touches" unchecked for the Scroll View in IB.


Following instructions found elsewhere about getting Scroll View to work with auto layout, I have a View "inside" the ScrollView . . . after some experimenting this arrangement appears to foul up the button function. When the button is on the View inside the Scroll View it will not highlight no matter what the touch settings on the superviews.

Found a case which other users may encounter, too.
Buttons in one of my several views using UIScrollView was not working after adding a ScrollView to programmatically generated view. Adding a missing layout constrain from last element in container view bottom to container view bottom solved issue. Did not have any build or runtime warnings about this "missing" constrain.

  • So if do not get button ( or other touch events ) to UI element on top of UIScrollView check bottom constrain of "containerView" ( the view on top of scroll ) - a missing bottom constrain to its last child element blocks touch events to any of containerView child elements.

Add a Comment