Group Group Group Group Group Group Group Group Group Forums

UIScrollView Tutorial: Getting Started

In this UIScrollView tutorial, you'll learn how to zoom large images, how to keep content centered while zooming, how to page content, and how to use Auto Layout with UIScrollView.

This is a companion discussion topic for the original entry at
1 Like

Your adjustInsetForKeyboardShow is written wrong. If you set userInfo to an empty dictionary you’re going to crash in the very next line when you force an invalid key to an NSValue. Your first two lines should really say:

guard let value = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }
let keyboardFrame = value.CGRectValue()

I was doing fine until

To define the size of the scroll view and fix the Auto Layout errors, you need to define its content size. Define the width of Container View to match the view controller’s width. Attach an equal width constraint from the Container View to the View Controller’s main view. For the height of Container View define a height constraint of 500.

I did not get it. Could you explain it more . “attach an equal width …”


1 Like

Good point gargoyle!
I’ll update the tutorial. Thanks.

Hello dehrab,

This is the part where you have to add a contraint from the view to its ancestor (not direct parent). See how to do it with this animated gif:


You suggested couple of times:
“uncheck Adjust Scroll View Insets in Attributes Inspector for Zoomed Photo View Controller.”
Why? What it does?

“Add top, leading, and trailing constraints to Container View like before. Add n width constraint of 300 to the image view.”
Why do I need to add width constraint if I already have leading and trailing constraints. Maybe did you mean height constraint?
And what are the constant values for top, leading and trailing constraints? It is not clear from the image.


1 Like

I had the same issue. I checked out the final project, and it appears it was a mistake: it is not a “width” constraint, it’s a height constraint.

1 Like

The tutorial was very easy to follow up to the section called Scroll view and Auto Layout. Setting constraints for the Container View seems somewhat ambiguous given the specificity earlier in the tutorial. I’m putting this tutorial on hold until some comments clear it up the ambiguity I’ve encountered or it’s updated.

@oronbz I’ve updated the tutorial:

  • indeed it was height constraints
  • I’ve removed the “uncheck Adjust Scroll View Insets” to make the tutorial simplier to follow

@dcdude the constraints Container /ScroolView /Content are the tricky part, see the animated gif in the comments for more “visual” help.

Hi @ckrych where is the previous tutorial that make paging with UIScrollView instead paging with UIPageViewController? great tutorial btw

Yu can’t I’m afraid. The latest update takes precedence.

Excellent tutorial! great job. Thank you very much, it was easy to read, very clear and informative. In fact I checked into this site thanks to the good tutorial.

However, I think the most complex were the autoLayout. Because they do not quite understand what they represent all elements of atributes inspector

Great tutorial, however, I found that the keyboard will not pop up with the tutorial. Also, I download the final project, it seems not working as well when I tap the textField. Any ideas? (iOS 9.3 on iphone 6s plus simulator)

It does not handle photos smaller than the scroll view bounds well. Any clue on how to work that around?


I found it out myself. If the image is smaller than the view’s bound the zoom scale that will be calculated for the “minimumZoomScale” and respectively used for the “zoomScale” properties will be smaller than “maximumZoomScale”. “maximumZoomScale” defaults to 1.0 and if “zoomScale” is bigger than that it is reverted back to 1.0. If you’d like the image to fully fill the x or y axis you have to make sure “maximumZoomScale” = “minimumZoomScale” or “zoomScale”; or bigger than that to enable zooming to bigger detail.

Hello everybody!

Just a little question regarding initial zoomScale which we set in updateMinZoomScaleForSize.

Why can’t we just set imageView.frame = scrollView.frame in viewDidLayoutSubviews keeping in mind that we set imageView.contentMode = .ScaleAspectFill or .ScaleAspectFit ?

Under Getting Started there is a link to a starter project in Objective C. Where is the starter project in Swift?

Hi, why the black background of UIPageControl can’t fill immediately?