The Startup
Published in

The Startup

Swift 5: How to Programmatically Add Images to a UIScrollView

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:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store