1

I have a UIScrollView with couple UILabels where I don't know the content of before execution. All the contents are embedded in a UIView (for the scrolling to work).

I also have a UIImageView that sits on top. So the layout if roughly like this

-------------------
|                 |
|   UIImageView   |
|                 |
|     UILabel     |
|                 |
|     UILabel     |
|                 |
|     UILabel     |
|                 |
|     UILabel     |
|                 |
-------------------

All UIViews are horizontally centered and have a leading and trailing space of 20. Each UILabel has a top and bottom space of 20.

And now comes the current problem. I want to have a varying top space for the UIImageView depending on what the screen size. If I have a fixed top space of lets say 20, then everything works. But if I try to set a range, lets say lessOrEqualTo 50 and greaterOrEqualTo 10, then I get a constraint error saying that the scroll view needs constraints for y position or height.

So how can I set the range of the top space without getting constraint error?

EDIT: These are my current constraints: Constraints

2 Answers2

0

You have two options. 1) You can have fixed image height and adjustable top space 2) You can have adjustable image height and fixed top space

Otherwise, How can the os determine those factors? Just think.

Kisoth Srinathan
  • 115
  • 2
  • 10
0

Actually, UIScrollView height is calculated by its contents. For example here,

scrollview.height = imageView.top + imageView.height + label1.top + label1.height + label2.top + label2.height + label2.bottom

This all things must be constant at the run time you can change their values but it cant be dynamic like constraints you have give lessOrEqualTo 50 and greaterOrEqualTo 10. Because in this case it is unable to predicate the scrollview.height as it described above.

Suggestions

  1. Take the outlet of top constraint of imageView.top and change its constant value.
  2. Give the multiplier to the imageView.top. It will automatically arranged according to your screen height.

If you need any further help or details you can ask.

Hope my answer helps you.

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51