This is the code that I use when setting up a
UIScrollView to contain multiple images that can
be scrolled through. I add this code in the
viewDidLoad or viewWillAppear
methods and then set the numberOfViews to the
number of images I want to display. This will also scale the
images to fit the size of the screen using an aspect fit
scaling. The example code loads images named "image1",
"image2" etc.
{% highlight objc %} -(void) setupScrollView { //add the scrollview to the view self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; self.scrollView.pagingEnabled = YES; [self.scrollView setAlwaysBounceVertical:NO]; //setup internal views NSInteger numberOfViews = 3; for (int i = 0; i < numberOfViews; i++) { CGFloat xOrigin = i * self.view.frame.size.width; UIImageView image = [[UIImageView alloc] initWithFrame: CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)]; image.image = [UIImage imageNamed:[NSString stringWithFormat: @"image%d", i+1]]; image.contentMode = UIViewContentModeScaleAspectFit; [self.scrollView addSubview:image]; } //set the scroll view content size self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width numberOfViews, self.view.frame.size.height); //add the scrollview to this view [self.view addSubview:self.scrollView]; } {% endhighlight %}