Home > Development > UIScrollView – A really simple tutorial

UIScrollView – A really simple tutorial

It’s very common to have a large amount of data that we want to display on the iPhone/iPod, but there’s no way that we can fit all at the same time on the screen (even on the iPad), that’s one of the functionalities of the UIScrollView.

The UIScrollView it’s a very versatile class, you can handle zooming, panning, scrolling, etc, and I have no intention of explaining all the properties and delegates (well, if you want to know something about the UIScrollView, fell free to request it in the comments, just let me know =D ), the documentation itself is pretty good, so you should give it a look.

In this post I’ll create a very simple project with three scrollable UIViews just to get familiar with the UIScrollView, and in the next post I intend to do some simple zooming example. So, the next  post will be about the UIScrollView as well.

The basics:

IMO, there’s 2 very important properties in UIScrollView, the contentSize and the contentOffset.

The contentsize is the width and height of your content, it’s a CGSize and a property of UIScrollView, let’s say that you have an image that’s 500×500, it would not fit on the iPhone screen, right? So, set your contentsize to 500,500. If you want to add more scrollable space at the bottom or at the top, you can use the property contentInset.top and contentInset.bottom, so you can add some extra space without changing the contentsize. And why would you need these insets? Well, if you have a UINavigationBar or a UIToolBar, like the photos app, you will use this.

Every UIScrollView has a scroll indicator (it’s visible by default, but if you want, you can hide it with showsHorizontalScrollIndicator and showsVerticalScrollIndicator), to give an indication of how far in the content you are, and you can change where the indicator starts using the scrollIndicatorInsets.top just like the contentInsets.

The contentOffset is the point that is currently visible, this point represents the top left of your screen. The contentOffset discards the contentInsets, so it can happen that the contentInset is negative, that’s not a problem.

The only thing that you need to have a functional UIScrollView is the UIView that you want to display and the contentSize of this UIView, so let’s start coding…

Create a View-based Application and name it SimpleScroll. Again, I always use the Window-based Application for my projects, but let’s pick the View-based just to speed things up.

Go to your SimpleScrollViewController class, in the loadView method and create your UIScrollView, we will create it with the same width and heigh as the view from SimpleScrollViewController

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
Enable the pagination
scroll.pagingEnabled = YES;

If the value of this property is YES, the scroll view stops on multiples of the view bounds when the user scrolls. The default value is NO.

Create all three UIViews
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}

The most important part in this for is to understand the yOrigin. This will place every UIView exactly where the previous UIView has stopped, in other words, each UIView will start at the end of the previous one.

Set the UIScrollView contentSize
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

The contentSize is just the sum of the widths of the three UIViews, if the width of each UIView is 320, and we have three UIViews, your contentSize width will be 920.

Add the UIScrollView to the SimpleScrollViewController UIView
[self.view addSubview:scroll];
[scroll release];
And you’re done :)
At the end, you should have something like this :
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
As you can see, I changed the background color of the SimpleScrollViewController UIView to red, just to make sure that you can distinguish the content of the UIScrollView.
That is the tutorial, pretty simple, huh? Now you can show all the data that you want on this small screen ;) Next post I’ll write about how to enable zooming using a UIScrollView.
Advertisement
  1. mike
    October 1, 2010 at 8:13 AM | #1

    Great Blog. I learned a lot

  2. November 13, 2010 at 8:11 AM | #2

    Since you’re using IB, the best solution is adding the UIScrollView directly in the xib file and declaring an outlet so you can use it later.

    • treviewz
      January 8, 2011 at 9:51 PM | #3

      Great tutorial and it works! But, can you tell me how do you add multiple images in the UIView named awesome?

      For example, displaying an image on each page that is scrolled.

      Thanks!

      • January 8, 2011 at 10:05 PM | #4

        You need to add the images in the UIScrollView in the correct position, like I did with the colors in this example, and then add the scrollview to the UIView.

  3. treviewz
    January 9, 2011 at 10:48 AM | #5

    Alright! Thanks alot. This has helped me. Really appreciated! I have finally manage to understand about UIView and UIScrollView.

  4. Brettman
    March 20, 2011 at 6:47 PM | #6

    Great tutorial. Just wanted to say thanks.

  5. March 23, 2011 at 11:01 PM | #7

    Thank you, this was very easy to understand. It just took minutes for me to implement and customize this. Good lookin out!

  6. Joe
    March 30, 2011 at 4:05 PM | #8

    Is it possible to tile UIViews built in IB instead of coding them?

  7. Joe
    March 30, 2011 at 8:51 PM | #10

    Cool. Ok then, Can you add xib files to a UIScrollView?

    • March 30, 2011 at 8:59 PM | #11

      Yes, you can create a xib file that it’s a UIView and do the exact same thing as I did in this example, but replace the UIView creation for the IBOutlet that it’s your UIView

  8. Joe
    March 30, 2011 at 9:51 PM | #12

    Thanks for the responses. Going back a step. I’ve been able to implement numerous scrolling UIImageView examples (which tile images) but have not been able to find out how to scroll tiled UIViews instead (to allow custom built pages with buttons, positioned alerts ect…).

    My main problem is knowing where best to place my custom IUViews. I’m guessing they would be better off in the nib file that contains the UIScrollView. If so, should they be nested inside the UIScrollView or does that not matter?

  9. Joe
    March 30, 2011 at 10:21 PM | #13

    Yeyhey, Just got your scroll app to work. That is the shortest and neatest piece of code I’ve seen that implements scrolling! No to find the illusive bit of code to replace your for loop contents :D

  10. Joe
    March 31, 2011 at 1:00 AM | #14

    Hi iDevZilla,

    I created 3 xib files and hooked up the viewcontrollers and can successfully insert a view by adding [awesomeView insertSubview:view1.view atIndex:0]; on the end of your for loop.

    I was expecting my view ‘view1′ to show up three times tiled. But it only appears on the last (3rd) page. Would you know why?

    Would be grateful for any help.

  11. Joe
    March 31, 2011 at 2:51 AM | #15

    My last comment to you (“At bloody last” thinks iDevZilla). I was able adapt your script to load up all three UIViews. Below is the final code.

    - (void)loadView {
    [super loadView];
    self.view.backgroundColor = [UIColor redColor];
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    scroll.pagingEnabled = YES;
    NSInteger numberOfViews = 3;

    view1.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [scroll addSubview:view1.view];
    view2.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    [scroll addSubview:view2.view];
    view3.view.frame = CGRectMake(self.view.frame.size.width*2, 0, self.view.frame.size.width, self.view.frame.size.height);
    [scroll addSubview:view3.view];

    scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
    [self.view addSubview:scroll];
    [scroll release];
    }

    If it wasn’t for you iDevZilla, i wouldn’t have continued trying to get UIViews to work in a UIScrollView.

    Cheers

  12. vaijayanthi
    April 28, 2011 at 10:30 AM | #16

    wow really good send ur next post url path.thanks for the wonderful tutorial.

  13. Qlimax
    September 21, 2011 at 2:20 AM | #17

    Thank you, fantastic tutorial!

  14. meriam
    November 15, 2011 at 3:13 PM | #18

    Thank you, you saved me today

  15. December 12, 2011 at 7:33 PM | #19

    You should link to the next post ( http://idevzilla.com/2010/10/04/uiscrollview-and-zoom/ ) so people don’t have to manually find it :-)

  16. Tal
    January 30, 2012 at 4:32 AM | #21

    Excellent tutorial!

    Thx!

  1. May 19, 2011 at 1:12 AM | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.