Disabling vertical scrolling in UIScrollView

后端 未结 12 1962
终归单人心
终归单人心 2020-11-30 20:02

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 no

相关标签:
12条回答
  • 2020-11-30 20:46

    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];
    }
    
    0 讨论(0)
  • 2020-11-30 20:47
    - (void)scrollViewDidScroll:(UIScrollView *)aScrollView
    {
        [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x,0)];
    
    }
    

    you must have confirmed to UIScrollViewDelegate

    aScrollView.delegate = self;
    
    0 讨论(0)
  • 2020-11-30 20:52

    Just set the y to be always on top. Need to conform with UIScrollViewDelegate

    func scrollViewDidScroll(scrollView: UIScrollView) {
            scrollView.contentOffset.y = 0.0
    }
    

    This will keep the Deceleration / Acceleration effect of the scrolling.

    0 讨论(0)
  • 2020-11-30 20:52

    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.

    0 讨论(0)
  • 2020-11-30 20:56

    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.

    scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
    
    0 讨论(0)
  • 2020-11-30 20:57

    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:

    1. You are not allowed or just don't want to use delegate.
    2. Your content height is larger than container height
    0 讨论(0)
提交回复
热议问题