Ios – Remove extra bottom padding from horizontal UIScrollView

autolayoutiosios8swiftuiscrollview

I have created a horizontally scrolling UIScrollView which contains UIButtons. I want the UIButtons to fill the height of the UIScrollView, so I've set up auto layout for the buttons: the top space and bottom space are set to Superview with a constant of 0. In Interface Builder it shows the correct UIScrollView frame size, but the buttons are 10 pixels too small – there's extra padding underneath the buttons. The scroll view's height is 44 pixels, the buttons height are 34 pixels.

I thought this might be because it preserves some space to show the horizontal scroll bar. But I have disabled Shows Horizontal Indicator in IB. I then attempted to programmatically remove the space via the code below but that didn't change anything.

self.automaticallyAdjustsScrollViewInsets = false
scrollView.contentInset = UIEdgeInsetsZero
scrollView.scrollIndicatorInsets = UIEdgeInsetsZero
scrollView.contentOffset = CGPointMake(0, 0)

EDIT: It must not be related to preserving space for a scroll bar. The scroll view's height is equal to the height of another button. When I change that button's height to change the scroll view height, the amount of bottom spacing increases. The buttons are always 34pt tall.

Another thing I tried was to remove the bottom space and add a fixed height constraint, but this results in an ambiguous scroll view height.

How can this padding be removed so that the buttons are the exact height of the scroll view's height?

Interface Builder:
enter image description here

Running the app:
enter image description here
(I temporarily unhid the scroll bar to show it here)
I need the white button to stretch in height to cover up the gray area beneath it.

Best Answer

I believe for UIScrollView to work with AutoLayout you'll need to wrap its subviews with an inner 'content' view as described in https://developer.apple.com/library/ios/technotes/tn2154/_index.html