如何始终使UIScrollView可滚动? [英] How to always makes a UIScrollView scrollable?
问题描述
我有一个 UIScrollView
。 scrollview.contentSize
设置为 UIScrollView
中所有子视图的高度。当 contentSize
的高度大于 UIScrollView
的高度时,它在拖动视图时完全滚动。当 contentSize
的高度小于 UIScrollView
的高度时,我拖动视图时不会发生任何事情。 / p>
我认为这是标准的行为,因为没有什么可以滚动。但我想, 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];
有没有更简单的方法?
UIScrollView
上有两个属性听起来像你想要的:
@property(nonatomic)BOOL alwaysBounceVertical; // default NO。如果YES和bounces是YES,即使内容小于bounds,允许垂直拖动
@property(nonatomic)BOOL alwaysBounceHorizontal; // default NO。如果YES并且bounces是YES,即使内容小于边界,也允许水平拖动
这也可以在IB中通过以下方式设置:检查水平反弹和/或垂直弹回框:
I have a UIScrollView
. The scrollview.contentSize
is set to the height of all subviews in the UIScrollView
. When the height of the contentSize
if greater than the height of the UIScrollView
, it is perfectly scrolling when dragging the view. When the height of the contentSize
is less than the height of the UIScrollView
nothing is happening when I drag the view.
I think that is standard behavior, since there is really nothing to scroll. But I would like, that the UIScrollView
is moving a bit anyway.
A possible solution is to ensure, that the height of the contentSize is never less than the frame.height plus a small margin (i.e. 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];
Is there a less hackish way to do this?
There are two properties on UIScrollView
that sound like what you want:
@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
Simply set one or both of them to YES
and you will get the bounce effect.
This can also be set in IB by checking the "Bounce Horizontally" and/or "Bounce Vertically" box(s):
这篇关于如何始终使UIScrollView可滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!