I found the basic operation of UIScrollViews
to be a little perplexing; it wasn’t obvious to me how to get a simple test case up and running. Therefore, I thought it might be useful to share such a test case with you.
The Goal
I want to create a large UIView
, containing a single (centered) UILabel
, and view/scroll that UIView
inside a UIScrollView
.
The Code
You know what? This is one of those cases where it’s just much, much easier to present a complete Xcode project. You can download it here: It’s built atop the View-Based Application template, with only a few modifications.
Some of the bigger changes:
- The main view controller gets a content view
IBOutlet
- The main view controller gets a quick-and-dirty scroll view accessor
- The main view controller sets its scroll view’s
contentSize
inviewDidLoad
- The main view controller’s NIB sets the view controller’s
view
to aUIScrollView
, and itscontentView
to aUIView
nested inside thatUIScrollView
I’d say that the nesting of the UIView
inside the UIScrollView
, and the setting of the UIScrollView's
contentSize
property, are the most important bits, and should command your attention.