Scroll View

A scroll view is a UI object that acts like a big canvas which displays a large area of content - more than will fit in a single view. This view can be any size (up to the size of its parent view) and lets the user swipe around the scroll view to move around the larger area in an interactive way.

With a scroll view, you must define the size of the overall scroll view within that view, and you decide on the size of the content area.

Adding a Scroll Sub-View

To add a scroll view to your viewController, first find the Scroll View object in the Object Library within Xcode.

Using the Scroll View

Dragging this into your view, you will see the scroll view is a small sub-view within the main view.

Using the Scroll View

Now, stretch the scroll view to the size you would like. The example (shown below) will stretch the scroll view to the same size as the view it was placed in (its parent view).

Using the Scroll View

Next, in the Document Outline area in Xcode, drag the scroll view outside of the main View Controller hierarchy - keeping it within the View Controller scene but keeping it outside of the main view in the storyboard - so we can edit the full area.I f we don't do this step, the borders of the main view will cut off the rest of the scroll view, making it impossible to lay out content.

Using the Scroll View

TIP

We will have to add the scroll view back in the view for it to render. We will do this in code later in the lesson.

Now, the scroll view is sitting outside the main view in the storyboard, letting you edit it much easier. When you select that scroll view in the storyboard and go to the Size Inspector on the right in Xcode, you can define the overall size of the content area.

Using the Scroll View

Now you can add the UI objects to this scroll view as you would in a normal view.

Using the Scroll View

After the layout is done, go to the Size Inspector on the right in Xcode with the scroll view selected, and set the size of the view back to its default. Even though you see the content get cut off, it is still there and accessible.

Using the Scroll View

Connecting and Setting Scroll View Properties

Now that the scroll view is set up in the storyboard properly, it's time to connect it to the viewController class and finish setting it up. First, you must connect the scroll view to an Outlet in your viewController class.

Start by selecting the main view in the storyboard and opening the Assistant Editor. This will show the storyboard and the viewController class at the same time. Press Control+click then drag from the scroll view to the top of your viewController class to create an Outlet.

Using the Scroll View

This pops up a dialog that lets you enter the name of the Outlet you are creating. Name it what you would like, make the Storage option set to weak, then click the Connect button.

Using the Scroll View

Now that the viewController class has an Outlet connecting the scroll view, it must be added as a sub-view to be rendered i(we removed it earlier by dragging it out of the main view several steps ago to help lay out the content). You can do this in the viewDidLoad() function using the following code:

view.addSubview(myScrollView)
1

The other thing you will need to do is tell the scroll view how big the content size is. This should be done in another function you can override called viewWillLayoutSubviews() and can be set like so:

myScrollView.contentSize = CGSize(width: 374, height: 1000)
1

You must enter the width and height values for the overall size of the scrollView's content area. In this case, it's using 374 and 1000. This means the extra content will need to be scrolled through vertically.

You may also want to manually set its overall dimensions and position within the main view. This is done by setting the scroll view's centre and bound size. In the example above we want to set it to the full size of the parent view which is set like this.

myScrollView.center = view.center
myScrollView.bounds = view.bounds
1
2

Using the Scroll View

Back to Week 6

Last Updated: 10/14/2020, 4:48:59 AM