Search code examples
iosxcodepaginationuiviewcontrolleruiscrollview

Xcode: UIScrollView with Paging Tweak


I found a great tutorial online that shows you how to page through different pages, well kind of. Here is the link to the tutorial here: http://www.iosdevnotes.com/2011/03/uiscrollview-paging/

The question I have is, how can I tweak his code so that instead of loading "colors" it loads different independent views or view controller files I create? Like I see it loads it's information from the NSArray, but how do you code it so that you include views or view controllers inside that array, and do you have to add anything else special to make this happen?

Here is the code I'm referring to from his tutorial which is in the implementation file:

- (void)viewDidLoad {
    [super viewDidLoad];

    pageControlBeingUsed = NO;

    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [self.scrollView addSubview:subview];
        [subview release];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);

    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = colors.count;
}

Solution

  • As a quick thought, you could try creating an array or similar of views instead of colours.

    Then in the for loop get the views out of the array and use this in the addSubView: message.

    This would work for a few views but would not scale well.

    I do something similar to this but with UIImageViews to be able to scroll a few images.