Try setting the contentSize's height to the scrollView's height. Then the vertical scroll should be disabled because there would be nothing to scroll vertically.
but if for some strange reason your contentSize should be higher than the UIScrollView, you can disable the vertical scrolling implementing the UIScrollView protocol method
float oldY; // here or better in .h interface
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
[aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, oldY)];
// or if you are sure you wanna it always on top:
// [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, 0)];
}
it's just the method called when the user scroll your UIScrollView, and doing so you force the content of it to have always the same .y
I updated the content size to disable vertical scrolling, and the ability to scroll still remained. Then I figured out that I needed to disable vertical bounce too, to disable completly the scroll.
On iOS 11 please remember to add the following, if you're interested in creating a scrollview that sticks to the screen bounds rather than a safe area.:
if (@available(iOS 11.0, *)) {
[self.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
If you set constraints for frameLayoutGuide.topAnchor and frameLayoutGuide.bottomAnchor to the same anchors of some subview of your scrollView then vertical scroll will be disabled and the height of the scrollView will be equal to the height of its subview.
A lot of answers include setting the contentOffset to 0. I had a case in which I wanted the view inside the scrollView to be centered. This did the job for me:
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.contentOffset.y = -scrollView.contentInset.top
}