Make UIScrollView Move Two Directions
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.
Create a ScrollView in IB
Drag a UIView (this is the content View) inside the scrollView (appears inside Scroll in the object hierarchy)
I had written a small memo for myself, on how to set constraints, hope it will help.
Building a ScrollView with its constraints
- it is the contentView which contains all of what will be displayed
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:
- 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 :
leading = 0
Define its dimensions by constraints relating to the Safe area
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.
Which content exactly ?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?
In the hierarchy (as seen in IB), you have:
ScrollView
View (the content view)
objects inside the content view (for instance, image)
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.