Swift 5: How to Programmatically Add Images to a UIScrollView

Eric Gustin
The Startup
Published in
1 min readJul 14, 2020

Scroll views allow for scrolling and zooming in your iOS App. Today we’re going learn about how to add images to them by taking advantage of their “contentSize” attribute. The content size of a UIScrollView refers to the width and height of its content view. When a UIScrollView’s content size is larger than the phone screen, then scrolling is enabled.

Step 1: Create a new Xcode project and navigate to ViewController.swift

Step 2: Create the UIScrollView and give it the appropriate properties.

Step 3: Create our UIImageViews and make them subviews of the UIScrollView. Once we do this, our final code looks like:

--

--

Eric Gustin
The Startup

I make tutorials and articles on Programming, iOS Development, and Mathematics github.com/EricGustin