UIScrollView disable scrolling in just one direction?

This works for me:

    -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
        scrollView.bounces = YES;
    }

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{

        if (scrollView.contentOffset.y < 0) {
            [scrollView setContentOffset:CGPointMake(0, 0)];
            scrollView.bounces = NO;
        }

        if (scrollView.contentOffset.y == 0){
            scrollView.bounces = YES;
        }

        else scrollView.bounces = YES;
    }

Fortunately, we can use scrollRectToVisible to avoid jittery behavior after the scroll has been limited:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > 60) {
        [scrollView setContentOffset:CGPointMake(0, 60)];

        CGFloat pageWidth  = scrollView.frame.size.width;
        CGFloat pageHeight = scrollView.frame.size.height;
        CGRect rect = CGRectMake(0, 0, pageWidth, pageHeight);
        [scrollView scrollRectToVisible:rect animated:YES];
    }
}

Turns out a simple solution was actually possible and easy:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > 60) {
        [scrollView setContentOffset:CGPointMake(0, 60)];
    }
}

The above solutions will reset you to zero,zero if the user accidentally scrolls vertically. Try this...

- (void)scrollViewDidScroll:(UIScrollView *) scrollView {
    if (scrollView.contentOffset.y > 0) {
        [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, 0)];
    }
}

Tags:

Uiscrollview