Search code examples
iosobjective-cuiscrollviewuilabel

UIScrollView with multiple multi-line labels and AutoLayout?


Is there a way to achieve this? I have tried literally everything and nothing has worked for me yet.

So basically what i want to do is the following: I have a scroll view with some labels in it. All the labels get their text from a server and I have set their number of lines to 0 so that they change their height according to the amount of text. However, this does not affect the scrollview content size(even though my labels have constraints set up to the bottom,top,leading and trailing of the scrollview) and the labels go off screen and I am unable to scroll down. Can someone point me in the right direction to how I would set up my constraints, my view hierarchy and etc?

Any help is much appreciated! :)


Solution

  • I would consider using UITableView instead, it has several benefits:

    1. It allows for reuse of cells, if all the cells look the same
    2. It manages recycling of cells when the number of values you're getting from the server increases (decreases memory pressure when number of cells becomes substantial)
    3. It allows for more flexibility with the content (it's quite often for design to change last second or to evolve over the course of the project)
    4. Most importantly, UITableView support auto sizing cells (as of iOS8), you need to specify the constraints between the label and the borders of the cell

    There are several resources to start with: