How To Use UIScrollView in Your iPhone App

Post image for How To Use UIScrollView in Your iPhone App

by mattjdrake on December 7, 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.

Implementing UIScrollView in Cocoa-Touch

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.

Add IBOutlets

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

	#import <UIKit/UIKit.h>

	@interface UseScrollViewViewController : UIViewController {
		IBOutlet UIScrollView *scrollView;
		UIImageView *imageView;
	}

	@property (nonatomic, retain) UIScrollView *scrollView;
	@property (nonatomic, retain) UIImageView *imageView;

	@end

Finish implementing the IBOutlet and property in the implementation file.

	#import "UseScrollViewViewController.h"

	@implementation UseScrollViewViewController
	@synthesize scrollView, imageView;

	- (void)dealloc {
		[super dealloc];
		[imageView release];
		[scrollView release];
	}

	@end

Adopt the Delegate Protocol

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.

	#import <UIKit/UIKit.h>

	@interface UseScrollViewViewController : UIViewController<UIScrollViewDelegate&gt {
		IBOutlet UIScrollView *scrollView;
		UIImageView *imageView;
	}

	@property (nonatomic, retain) UIScrollView *scrollView;
	@property (nonatomic, retain) UIImageView *imageView;

	@end

Implement the Delegate Method viewForZoomingInScrollView

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

	#import "UseScrollViewViewController.h"
	@implementation UseScrollViewViewController

...

	- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
		return imageView;
	}

...

	@end

Create the Image View

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.

	- (void)viewDidLoad {
	    [super viewDidLoad];
		UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Beer-Sign-On-Wall.jpg"]];
		self.imageView = tempImageView;
		[tempImageView release];

	}

Set the UIScrollView Properties

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.

	- (void)viewDidLoad {

...

		scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
		scrollView.maximumZoomScale = 4.0;
		scrollView.minimumZoomScale = 0.75;
		scrollView.clipsToBounds = YES;
		scrollView.delegate = self;
		[scrollView addSubview:imageView];
	}

Add Scroll View in Interface Builder

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!

Here Is How To Fast Track Your Objective-C Programming Skills…


Click here or the box to the right to access to your online workshop now…

Each lesson comes packed with comprehensive video, source code and text. When appropriate I include hands-on exercises. Check out the list below to see what is specifically covered in each lesson:

Module 1 – Getting Started With iPhone App Development

- Lesson 1 – Overview of iPhone OS
- Lesson 2 – Introduction to Tools: XCode, Interface Builder & iPhone Simulator
- Lesson 3 – Your First App
- Lesson 4 – Super-Charge XCode

Module 2 – Learn How to Program in C

- Lesson 1 – What is Programming?
- Lesson 2 – C Programming Basics and Specifics
- Lesson 3 – Functions
- Lesson 4 – Variables and Arrays
- Lesson 5 – Program Flow
- Lesson 6 – Loops
- Lesson 7 – Complex Data with Struct
- Lesson 8 – Putting It All Together

Module 3 – Master Object Oriented Programming With Objective-C

- Lesson 1 – What is Object Oriented Programming?
- Lesson 2 – Objects
- Lesson 3 – More Strings, Lists and the For Each Loop
- Lesson 4 – Memory Management
- Lesson 5 – Designing Your Own Classes
- Lesson 6 – Extending Classes With Categories
- Lesson 7 – Protocols & Key-Value Coding

Module 4 – No-BS Cocoa-Touch With iPhone SDK

- Lesson 1 – Overview of Cocoa-Touch + Model-View-Controller
- Lesson 2 – Using Interface Builder (The View)
- Lesson 3 – Target-Action and the View in Code
- Lesson 4 – Delegation
- Lesson 5 – Super-Charging Your View With Interface Builder
- Lesson 6 – Model & App Architecture

Click here or the box to the right to access to your online workshop now

Please share this if you like it!
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • MySpace
  • Ping.fm
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Yahoo! Bookmarks

{ 2 comments… read them below or add one }

1 newtoiphoneNo Gravatar February 23, 2010 at 9:14 pm

Hi,
Is there a specific reason for not using IB to add your image view with the image to the scrollview?

I tried to add an imageview to the scrollview, added an image using attributes inspector to the image view, connected the file’s owner using an outlet. Then I changed the ViewDidLoad method to remove image view creation and addition statements. But my example doesn’t work. Can you suggest other changes that I may have to make?

Thank you.

2 olsonvoxNo Gravatar May 10, 2010 at 1:56 pm

This is an AWESOME tutorial. Very easy to understand and EXACTLY what I was looking for. One thing. When the image loads into the UIScrollview, is there a way to make it look to fit the screen? I would like the image loaded to by default zoom to the size of the window it is in. Then allow zooming in. I don’t want to start zoomed in.

Does that make sense?

Previous post:

Next post: