How To Use UIScrollView in Your iPhone App

Matthew Campbell, December 7th, 2009

Sometimes you would like to be able to display an image or view that is larger than the iPhone or iPod screen. UIScrollView gives you this ability plus the feature of zooming using the pinch gesture. This video will show you how to implement this in your own iPhone app. Source Code follows video.

This example starts with a View Based Application with the image already in the Resources group. You can create this yourself using XCode’s “New Project” menu item.

Select the view controller interface file and add the scroll view IBOutlet and the image view property:

Finish implementing the IBOutlet and property in the implementation file.

To use UIScrollView we must adopt the UIScrollViewDelegate protocol. Once we do our view controller may act on behalf of our scroll view. Simple add this after the UIViewController sublcass: <UIScrollViewDelegate&gt.

Implementing this method will allow the scroll view to provide the pinching and zooming behavior demonstrated in the video.

The image view will be used to display the image on the view. This is pretty straightforward: you will simple create the object and set it to the property we defined earlier in the viewDidLoad method.

Since we are using Interface Builder to add the scroll view we do not need to create it here. But, we will be setting some of the scroll view properties. Note that we add the image view to the scroll view’s subview collection.

Now all you need to do is add your scroll view in interface builder and hook it up to the IBOutlet you defined in the view controller!

Discuss in the comments below!