如何始终使UIScrollView可滚动?

dhr*_*hrm 28 cocoa-touch objective-c uiscrollview ios

我有一个UIScrollView.该scrollview.contentSize设置在所有子视图的高度UIScrollView.当contentSizeif的高度大于高度UIScrollView时,拖动视图时完全滚动.当我拖动视图时,当contentSize高度小于高度时UIScrollView没有发生任何事情.

我认为这是标准行为,因为没有什么可以滚动.但我想,UIScrollView无论如何,这有点移动.

一个可能的解决方案是确保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];
Run Code Online (Sandbox Code Playgroud)

是否有一种不那么强硬的方式来做到这一点?

NJo*_*nes 74

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
Run Code Online (Sandbox Code Playgroud)

只需将其中一个或两个设置为,即可YES获得反弹效果.

这也可以通过选中"水平弹跳"和/或"垂直弹跳"框在IB中设置:

在此输入图像描述

  • 请注意,除了"水平弹跳"或"垂直弹跳"之外,还必须选中独奏"弹跳"复选框. (4认同)

Tia*_*des 8

迅速:

scrollView.alwaysBounceVertical = true
scrollView.alwaysBounceHorizontal = true
Run Code Online (Sandbox Code Playgroud)