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.
滚动视图可用于在iOS App中滚动和缩放。 今天,我们将学习如何利用图像的“ contentSize”属性向其添加图像。 UIScrollView的内容大小是指其内容视图的宽度和高度。 当UIScrollView的内容大小大于电话屏幕时,则启用滚动。
Step 1: Create a new Xcode project and navigate to ViewController.swift
步骤1:创建一个新的Xcode项目并导航到ViewController.swift
Step 2: Create the UIScrollView and give it the appropriate properties.
步骤2:创建UIScrollView并为其提供适当的属性。
Step 3: Create our UIImageViews and make them subviews of the UIScrollView. Once we do this, our final code looks like:
步骤3:创建我们的UIImageViews,并使它们成为UIScrollView的子视图。 完成此操作后,最终代码如下所示:
翻译自: https://medium.com/swlh/swift-5-how-to-programmatically-add-images-to-a-uiscrollview-ee5b97aa2e00