Make UIScrollView Move Two Directions

I am wondering if there is a way to get UIScrollview to scroll horizontally and vertically in storyboard instead of of just one direction?
View scrolls in a direction only if the content is larger in a direction than the scrollView.
What you do usually:
  • create a View (will be the content view)

  • place objects (as image, labels, …) inside, that will scroll

  • embed the contentView in a Scroll View.

You can also create the ContentView and place inside ScrollView (if you have not selected to embed)
  • Create a ScrollView in IB

  • Drag a UIView (this is the content View) inside the scrollView (appears inside Scroll in the object hierarchy)

In any case, scroll capability depends on the relative size of ContentView and scrollView.

I had written a small memo for myself, on how to set constraints, hope it will help.

Building a ScrollView with its constraints

  • ScrollView is composed of a scrollview, a content view and objects in content view to be scrolled.

  • One need to have the concept clearly in mind:

        - it is the contentView which contains all of what will be displayed
        - the scroll is a window that "passes over" the content to reveal part of it
        - and yet contentView is set INSIDE ScrollView, which may be misleading

it will scroll in one direction only if the content is larger than the scrollView in that direction
the contentView should be set to include « exactly » all that should be showable. If too large, it will scroll beyond the content and we will see an empty area

For the ScrollView :

  • Define its dimensions by constraints relating to the Safe area

            leading = 0
            trailing = 0
            top = maybe non zero if you scroll a part of the screen
            bottom = 35 (here to leave room for some buttons below)
you can keep the content layout and frame layout ON

Define now the constraints of UIView relative to the scrollView (Superview)
         leading = 0
         trailing = 0
         equalWidth with Scroll if we don't want horizontal scroll. DON'T do this if you want horizontal scroll
          top = 0
          bottom = 0
          equalHeight with Scroll if we don't want vertical scroll. DON'T do this if you want vertical scroll
    Define the dimension constraints of the ContentView to correspond to the complete content you have to put inside (the content that will scroll)
          height =
          width =
Note: this is necessary to eliminate red warnings from IB
Place objects in the ContentView
for each
Define the constraints relative to the ContentView.


Okay I see what your saying but how come in my experience the view can scroll in a direction where the content is smaller instead of larger?

I see what your saying but how come in my experience the view can scroll in a direction where the content is smaller instead of larger? 

Which content exactly ?
In the hierarchy (as seen in IB), you have:
  • ScrollView

  •      View (the content view)

  •            objects inside the content view (for instance, image)

It is the size of the contentView, not the size of the image inside the content view.
what I mean by content is the content view.
I tried your solution but I can only get it to scroll vertically. Even if I were to make the leading of the scrollview larger it still would not give me horizontal scroll.
Without viewing the details of your setup, very hard to say.

You have certainly something ill defined, as I used what I described in many cases and it worked perfectly.

If you want to post an email for sharing files, we can do it.
Make UIScrollView Move Two Directions
 
 
Q