4

I've read a lot of manuals/tutorials on ScrollViews with vertical scrolling, but havent figured out how to solve my problem so far.

You can see my ScrollView visual plan here:

ScrollView plan

The most helpful answer was here, on StackOverflow: https://stackoverflow.com/a/30282707/3718319

Accodring to it, I made the following:

My ScrollView(makred red on my screenshot) has got 0,0,0,0 constraints and equal width to ViewController's view. Inside my ScrollView I made a ContentView(makred orange on my screenshot), that contains (makred green on my screenshot):

1) ImageView with constant height and pinned top/left/right to ContentView.

2) View with constant height and pinned top to ImageView, left/right to ContentView.

3) View with constant height and pinned top to the View above it, left/right to ContentView.

4) TextView pinned top to the View above it, left/right to the ContentView and pinned bottom to the ContentView.

So, all the vertical constraints are set, and my scrollable content height should change according to height of text view. That's what is my goal.

But I got error from storyboard, telling: Need constraints for: Y position or height.

Could you please help me solve this problem?

Thanks!

Community
  • 1
  • 1
oleskii
  • 366
  • 3
  • 14

2 Answers2

3

The reason storyboard throws those warning is because it cannot calculate the scroll view's content size. With your first set of constraint in content view, it has an ambiguous height.

lets say for example that you set each height constraint to 100 and pinned constraint have 20 space value, the frame for each subview inside content view would be like this:

  1. ImageView (20, 20, view.Width, 100)
  2. View (20, 140, view.Width, 100)
  3. View (20, 260, view.Width, 100)
  4. TextView (20, 380, view.Width, ??)

since your textView have top and bottom constraint, the height of it will vary, and content view's height will also vary according to textView height.

However, textView also have scroll feature, which will make it difficult to calculate its height related to content view without additional constraint.

Since you disable its scroll function inside textView, storyboard no longer have any difficulty calculating each subview frame which is why the warning is gone.

optionally, you can enable textView's scroll function and giving height constraint to it which will give enough info for storyboard to calculate each subview frame.

seto nugroho
  • 1,359
  • 1
  • 13
  • 20
  • Thanks, for details. Hope your answer will help people getting to UIScrollViews! – oleskii Nov 30 '15 at 16:59
  • Disabling scrolling was the key here - thanks for the detail :-) – richsage Feb 25 '16 at 10:30
  • I had the same problem, but I didn't have a TextView, I had a label that was pinned TOP, LEFT and RIGHT, but no bottom as my last element. Pinning the bottom and adjusting the frame of the contentview solved my problem and the scrollview works perfectly now. Tks! – gmogames Apr 24 '16 at 20:43
2

I've just figured out, that I had to uncheck "Enable scrolling" on my textfield. That makes sense.

I've already tried it before, but it didn't help me before I've made this storyboard ViewController from a scratch.

oleskii
  • 366
  • 3
  • 14