I have a UIScrollView
created in interface builder with a bunch of UITextView
and UIImageView
objects already added. I have it connected to an IBOutlet
, and under -(void)viewDidLoad
I set its content size to 1200x2000
. User interaction Enabled, Multitouch, Scrolling Enabled and shows vertical and horizontal scroll indicators are all set to YES
. When I launch the app in the iOS simulator, it doesn't scroll. What could possibly be the cause of this behavior?

- 6,931
- 7
- 35
- 51
-
post the initialization code? – lead_the_zeppelin Nov 04 '13 at 15:41
-
There is not any initialization code, I believe that the UIScrollView is automatically initialized by IB. All I did code-wise was to set contentSize property of the scrollView – 67cherries Nov 04 '13 at 15:42
-
override & check the UIScrollView Delegate methods to check whether the UIScrollView is actually getting the touch events or some other view is actually eating up all the events.. for e.g scrollViewWillBeginDragging: – Dinesh Nov 04 '13 at 15:44
-
I know that the UIScrollView is receiving the touch events because it bounces. I can pull the content up, but it bounces back to its original position. – 67cherries Nov 04 '13 at 15:46
-
Can you post the code what actually you are doing in your code. – Dinesh Nov 04 '13 at 15:47
-
This is what I'm doing in code: `_mainScrollView.contentSize = CGSizeMake(1200, 2000);` – 67cherries Nov 04 '13 at 15:48
-
are the views getting laid out to be longer than the default height? – lead_the_zeppelin Nov 04 '13 at 15:50
-
Did you add constraints to the UIScrollView to have the edges of the scroll view attached to the UIView that it's contained within? – Brian Shamblen Nov 04 '13 at 15:50
-
did u have used AutoLayout? if yes then keep these points in mind. http://stackoverflow.com/questions/18006780/ios-autolayout-causing-uiscrollview-to-not-scroll and also https://developer.apple.com/library/ios/technotes/tn2154/_index.html – Satish Azad Nov 05 '13 at 05:10
6 Answers
viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.
Are you using autolayout? If so, check out the following answer (and forget 1.): UIScrollView doesn't use autolayout constraints.
-
I tried the first one, and that didn't help. I checked out the other answer, and wrote this really quick code to implement it: `- (void)viewDidLoad { // Do any additional setup after loading the view. _mainScrollView.contentSize = CGSizeMake(4000, 4000); for(int i=0; i<_mainScrollView.subviews.count;i++){ UIView *newSubview=[_mainScrollView.subviews objectAtIndex:i]; newSubview.translatesAutoresizingMaskIntoConstraints=NO; } [super viewDidLoad]; }` – 67cherries Nov 04 '13 at 15:57
-
@Nenad M: Why forget No. 1? The pure auto layout approach seems still to require setting the `contentSize`. Perhaps if you take the mixed approach you don't need that, but I haven't tried that yet. – testing Nov 27 '14 at 09:31
-
@timquinn: And what are the secrets? One secret seems to be [this answer](http://stackoverflow.com/a/16029013/426227). – testing Nov 27 '14 at 09:32
The best way I know how to overcome this problem without resorting to writing code is that it all can be done within a storyboard and here is how:
- Leave autolayout enabled.
- Make a generic UIView a subview of the UIScrollView. 2.1)Put all your View components inside this generic UIView.
- Make a autolayout constraint between a subview and the UIScrollView. You must pin the right and/or bottom of the last subview to the right and/or bottom of the UIScrollView. This is how the UIScrollView knows the content size.

- 17,418
- 8
- 58
- 76

- 362
- 4
- 9
-
Thank you so much! This is the only way I have found to work with a UIScrollView in iOS 7 Landscape with Autolayout!!!!!!! :) Thumbs up! – Jack Solomon Mar 07 '14 at 23:04
viewDidLoad
is indeed too soon, but viewDidAppear:animated
might not be late enough either. The ideal place to set the contentOffset
is inside viewDidLayoutSubviews
.

- 55,636
- 19
- 86
- 87
If you're using Autolayout, make sure you put the code to set Content size within
viewDidLayoutSubviews

- 6,167
- 4
- 38
- 44
I use a custom view on the UIScrollView, for pull to refresh function, and the scroll view is not scrollable. I try set contentSize
with AutoLayout
, and try set it by codes too. The scroll view still cannot scroll.
After spending 3 ~ 4 hours, I solve it by contentInset
. It will tell scroll view to add some extra space for scrolling.
override func viewDidLayoutSubviews() {
// Add extra 15pt on the `top` of scrollView.
//self.scrollView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
// Add extra 2pt on the `bottom` of scrollView, let it be scrollable. And the UI is more beautiful in my case.
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 2, 0)
}
Reference docs:
This is a good walk-through of scroll-views in case anyone forgot after not using them for a while: https://medium.com/@pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7.
Basically, make sure you have view -> scrollview -> view, like so:
then,
1. Set scroll view constraint (top, bottom, leading and trailing) to (0,0,0,0).
2. Set inner view constraint (top, bottom, leading and trailing) to (0,0,0,0).
3. Set inner view to have equal width and equal height with parent view.
4. Select height constraint of inner view and set priority to low (250).

- 1,782
- 15
- 23