Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to always makes a UIScrollView scrollable?

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?

like image 430
dhrm Avatar asked Feb 25 '12 17:02

dhrm


1 Answers

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):

enter image description here

like image 100
NJones Avatar answered Oct 21 '22 14:10

NJones