There is an option in IB to uncheck vertical scrolling on a scrollview, but it doesnt seem to work.
How can the scrollview be set to only scroll horizontally, and not vertically in the code rather than IB?
yes, pt2ph8's answer is right,
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
-(void)scrollViewDidScroll:(UIScrollView *)aScrollView;
just add this in your UIViewController
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
From iOS11 one can use the following property
let frameLayoutGuide: UILayoutGuide
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.
The most obvious solution is to forbid y-coordinate changes in your subclass.
override var contentOffset: CGPoint {
get {
return super.contentOffset
}
set {
super.contentOffset = CGPoint(x: newValue.x, y: 0)
}
}
This is the only suitable solution in situations when: