-2

I have an app using scroll view on a page with numerous icons within that scroll view. I don't understand auto layout very well, but I have constrained every single icon to its current canvas value to the view (after measuring and placing each icon in the correct spot). Why are there still conflicts (188 to be precise) and unambiguous positions?

As an example of what I have done in case the above is not clear, I have placed an icon in the top left of the scroll view. I have then constrained that icon on top, bottom, left and right of its current canvas value from the view.

Mi Ch
  • 11
  • 5
  • We need to see the constraints to be able to help. I recommend do create a simple example showing the issue and update the question with this example and the reported conflicts. – shallowThought Jun 02 '17 at 11:48
  • Before moving to scrollView with autolayout i suggest you play at least two days with autolayout without scrollView.Because. using autolayout in scrollView is confusing. – elk_cloner Jun 02 '17 at 12:02

2 Answers2

0

This extremely hard to explain without showing (as in "live-demonstrating"), but I'll give it a try: Autolayout for Scrollviews works differently than you probably think. You can't simply specify where to place its contents in relation to its bounds like that. After all, the "canvas" of the scroll view determines its size from the content you scroll over, whereas the size of the actual scroll view (lets call it its viewport, or "window onto the canvas") is determined in a different way (for example by constraining it to the elements around it).

This the opposite of how autolayout works for other views, since they are usually constrained by the size of their parent (up to the top view of a view controller and/or the screen bounds).

What you want to do is add all your items in a single view. From the smallest known size, constrain them to one another. Finally their container has a size resulting from its contents, i.e. your items. This is then the size of the "canvas" of the scroll view, so add it and constrain its position somehow (for example centering it horizontally and vertically). Now, the actual size of the scroll view and its position should be constrained by elements surrounding it.

It might take some thinking and training, so maybe you will want to test around in a small training project that only has a limited number of views to figure it out.

Gero
  • 4,394
  • 20
  • 36
0
  1. Add scroll view on the screen with constraints to parent view
  2. Add content inside of scroll view
  3. Put constraints between added view and parent view (NOT SCROLL VIEW)
Dejan Zuza
  • 359
  • 1
  • 6
  • 14