how do I use UIScrollView in Interface Builder?

Viewed 65553

While I've used UIScrollView successfully in the past by manipulating it programmatically, I'm having trouble getting it to work by setting it up exclusively in Interface Builder.

I have a simple "about" page in my iPhone app. It has a UITextView, some icons, and links to my other apps. I have added all of these views to my UIScrollView, arranging them so that their total size is > 480. When I launch my app, the scrollview displays only the contents that fit on the screen, and nothing scrolls.

Is it possible to do this entirely via IB, or must I manipulate the contentSize via code?

18 Answers

You forgot to set the contentSize property of the UIScrollView. Strangely enough you can not do this from Interface Builder. You will have to do it from the view controller managing this scroll view.

You can do it entirely in Interface Builder without setting the "contentSize" property in code.

  1. Put only one View in Scroll View. The Scroll View should only has this child view. You can name it as Content View. And put all contents inside this View later.
  2. Align four edges of this View to the Scroll View (the View's superview) with zeros.
  3. Set the width and height of this View. The width and the height will be implicitly treated as the "contentSize" of the Scroll View.

Simply put, the width and height of this View define the "contentSize" of the Scroll View. Because this View is the only content of the Scroll View, the size of this View is the content size of the Scroll View. It is quite reasonable.

I learned it from this tutorial: https://riptutorial.com/ios/example/12812/scrolling-content-with-auto-layout-enabled

Related