2

I am using the Xcode storyboard interface builder and I created a view controller with a UIScrollView that only has a single child UIView. This child UIView has many labels/buttons etc attached and exceeds the screen size height wise, however when I run my app, the view does not scroll for some reason.

Here is a picture of the ViewController structure: UIScrollView

What am I doing wrong?

Bradsta
  • 157
  • 1
  • 12

2 Answers2

4

You need to set the contentSize property of the UIScrollView. You can do this programatically (e.g. in viewDidLayoutSubviews) or under User Defined Runtime Attributes in the identity inspector panel in interface builder.

The actual size is dependent on your content. You can calculate it programatically using something like:

CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
    contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect;

which will just find the smallest CGRect that contains all the frames of the subvies of the UIScrollView.

michaelrccurtis
  • 1,172
  • 3
  • 14
  • 16
1

You can set the contentSize programmatically, but you don't need to and shouldn't have to. Instead, ensure that you have enough constraints that the scroll view's child view can calculate an exact total width and height. If you don't have enough constraints defined to do this, then Interface Builder will show you an error.

NRitH
  • 13,441
  • 4
  • 41
  • 44
  • This is true. The problem I had was that I was using a UITabBarController and didn't realize that the UIScrollView did not take the tab bar into account when determining the content size. I had to resize the `contentSize` of the UIScrollView a bit in order to account for this and I did so within `viewDidLayoutSubviews()` as putting it in `viewDidLoad()` did not work for me. Thanks for all the help everyone! – Bradsta Aug 27 '15 at 00:13