I have created a UIScrollView and added subviews to it in Interface Builder, but I am not able to get it to scroll. I have looked at other similar questions, but I have set my contentSize appropriately. In fact, I have even tried setting incredibly large contentSize and it fails to make a difference. What am I missing? Note that the UIScrollView is an IBOutlet:
@property (strong, nonatomic) IBOutlet UIScrollView *scroll;
This is in my viewDidLoad:
self.scroll.scrollEnabled = YES;
[self.scroll setContentSize: CGSizeMake(2400,8000)];
Here are screenshots from Interface Builder:
Here is the view while running, but any attempt at horizontal or vertical scrolling at any point does not work.
Without any controls inside the content view, or if there are no placeholder width and height constraints on the content view, the scroll view cannot determine the content size. Ignore the errors for now.
These constraints tell the UIScrollView the boundaries of your content (sets the contentSize of the UIScrollView ). You usually only want your content to scroll in one direction. In my case, I want the scroll view to scroll vertically. Therefore, I need to set the width of my single content view to be the width of the scroll view.
In interface builder, drag a UIScrollView and add it as a subview of the view controller’s main view. Now add some constraints to your scroll view to place it where you want it in your main view.
The default value is 0 for top, bottom, right, and left. contentSize is the size of the content within the UIScrollView and how long it can be within scrollView. Sometimes this is dynamic like pagination or static like a contact list. This might be changing at runtime as well. This can be set programmatically as well.
Turning Auto Layout off works, but that's not the solution. If you really need Auto Layout, then use it, if you don't need it, turn it off. But that is not the correct fix for this solution.
UIScrollView
works differently with other views in Auto Layout. Here is Apple's release note on Auto Layout, I've copied the interesting bit (emphasis mine, at third bullet point):
Here are some notes regarding Auto Layout support for UIScrollView:
- In general, Auto Layout considers the top, left, bottom, and right edges of a view to be the visible edges. That is, if you pin a view to the left edge of its superview, you’re really pinning it to the minimum x-value of the superview’s bounds. Changing the bounds origin of the superview does not change the position of the view.
- The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view.
- The constraints on the subviews of the scroll view must result in a size to fill, which is then interpreted as the content size of the scroll view. (This should not be confused with the intrinsicContentSize method used for Auto Layout.) To size the scroll view’s frame with Auto Layout, constraints must either be explicit regarding the width and height of the scroll view, or the edges of the scroll view must be tied to views outside of its subtree.
- Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.
Apple then goes on to show example of how to correctly use UIScrollView
with Auto Layout.
As a general rule, one of the easiest fix is to create a constraint between the element to the bottom of the UIScrollView. So in the element that you want to be at the bottom of the UIScrollView, create this bottom space constraint:
Once again, if you do not want to use Auto Layout, then turn it off. You can then set the contentSize
the usual way. But what you should understand is that this is an intended behaviour of Auto Layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With