UIScrollView

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 in viewDidLoad
  • The main view controller’s NIB sets the view controller’s view to a UIScrollView, and its contentView to a UIView nested inside that UIScrollView

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.

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • HackerNews
  • del.icio.us
  • Google Bookmarks
  • Slashdot
This entry was posted in iPhone. Bookmark the permalink.

Comments are closed.