如何总是使UIScrollView滚动?

我有一个UIScrollViewscrollview.contentSize被设置为UIScrollView中所有子视图的高度。 当contentSize的高度大于UIScrollView的高度时,拖动视图时会完全滚动。 当contentSize的高度小于UIScrollView的高度时,我拖动视图时没有任何事情发生。

我认为这是标准的行为,因为没有什么可滚动的。 但是我想, UIScrollView正在移动一下。

一个可能的解决scheme是确保contentSize的高度永远不会小于frame.height加上一个小的边距(即5px):

 CGSize scrollSize = self.frame.size; int defaultHeight = self.frame.size.height + 5; int contentHeight = self.webView.frame.origin.y + self.webView.frame.size.height; scrollSize.height = MAX(defaultHeight, contentHeight); [self.scrollView setContentSize:scrollSize]; 

有没有一个不太危险的方法来做到这一点?

UIScrollView上有两个属性听起来像你想要的:

 @property(nonatomic) BOOL alwaysBounceVertical; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically @property(nonatomic) BOOL alwaysBounceHorizontal; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag horizontally 

只需将其中一个或两个都设置为YES ,您将获得反弹效果。

这也可以在IB中通过检查“水平反弹”和/或“垂直反弹”框来设置:

在这里输入图像说明