37

I've been having a very hard time finding good examples of UIScrollView. Even Apple's UIScrollView Suite I find a bit lacking.

I'm looking for a tutorial or example set that shows me how to create something similar to the iPhone Safari tab scrolling, when you zoom out from one browser window and can flick to others.

But I'm having a hard time just getting any old view showing within a scroll view. I have a view set up with an image in it, but when I add it to the scroll view, I only get a black rectangle, no matter what I put in the view I add.

Any links or code snippets would be great!

M. Ryan
  • 6,973
  • 11
  • 52
  • 76
  • Can you explain what you mean by iPhone Safari tab scrolling? Is that after you click on the bottom-right page button and then you get a UIPageControl that let's you flick to other browser windows? – mahboudz Oct 21 '09 at 04:46

3 Answers3

46

Here is a scroll view guide from Apple

The basic steps are:

  1. Create a UIScrollView and a content view you want to put inside (in your case a UIImageView).
  2. Make the content view a subview of the scroll view.
  3. Set the content size of the scrollview to the frame size of the content view. This is a very important step that people often omit.
  4. Put the scroll view in a window somewhere.

As for the paging behavior, check out UIScrollView’s pagingEnabled property. If you need to scroll by less than a whole page you’ll need to play tricks with clipsToBounds, sort of the reverse of what is happening in this StackOverflow question.

Community
  • 1
  • 1
Ben Stiglitz
  • 3,994
  • 27
  • 24
  • EDIT: found my question here: http://stackoverflow.com/questions/1135163/how-do-i-use-uiscrollview-in-interface-builder - and sadly the answer is "no, Apple forgot to enable it". ORIGINAL COMMENT: Is there any way to do STEP 3 above from within Interface Builder? I've been doing it in code, unable to find a way in IB. It's odd - basically, it seems Apple is forcing you to write code if you ever want a UIScrollView to actually work? – Adam Feb 21 '10 at 16:39
  • 3
    Quick note: the link to the example is no longer valid, and is now redirecting to various spam sites. – Brock Boland Jul 18 '13 at 02:43
  • 2
    Step 3:To set content size, add `[self.scrollView setContentSize:CGSizeMake(contentView width,contentView height)];` into the `viewDidAppear` method. – jithin Nov 29 '15 at 06:38
  • You can do this in Interface Builder by using the 'User Defined Runtime Attribute'. Key Path is 'contentSize', and the type is 'Size'. This saves having to have an IBOutlet if you aren't using a view controller. – AndyTang May 23 '16 at 15:56
16
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

NSInteger viewcount= 4; 
for (int i = 0; i <viewcount; i++) 
{ 
CGFloat y = i * self.view.frame.size.height; 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, self .view.frame.size.height)];      
view.backgroundColor = [UIColor greenColor]; 
[scrollview addSubview:view]; 
[view release]; 
}
scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount); 

For more information Create UIScrollView programmatically

Hema
  • 171
  • 1
  • 6
4

New 3/26/2013 I stumbled on an easier way to do this without code (contentSize)

https://stackoverflow.com/a/15649607/1705353

Community
  • 1
  • 1
Dickey Singh
  • 713
  • 1
  • 8
  • 16