UIScrollView disable scrolling in just one direction?

前端 未结 10 1811
暗喜
暗喜 2020-12-13 09:40

I\'ve been unable to find an answer for this (maybe someone has hacked a solution together).

Is it possible to disable scrolling in a UIScrollView in one direction?

相关标签:
10条回答
  • 2020-12-13 10:29

    Turn Off the auto layout. I had the same issues only fixed after disabling auto layout in the content view. Xcode 11 . Automatic -> Translate Masks to Constraints

    0 讨论(0)
  • 2020-12-13 10:32

    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];
        }
    }
    
    0 讨论(0)
  • 2020-12-13 10:33

    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;
        }
    
    0 讨论(0)
  • 2020-12-13 10:35

    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)];
        }
    }
    
    0 讨论(0)
提交回复
热议问题