1

I have a multiline label inside a scrollview. I set up the content size, let's say to scrollView.contentSize.height = 2000 But the view doesn't scroll. There is barely any code in the project. What is going wrong?

enter image description here

enter image description here

The only thing is that I don't have constrain for the height of the label, because it will vary depending on the length of text.

Ilia Tikhomirov
  • 549
  • 1
  • 4
  • 16
  • Since scroll view needs to know it's contentSize it is required to set autolayout constrains from top to bottom, so that height is not ambiguous. In your case you can set label's height constraint and update it manually from code. – alexburtnik Oct 11 '16 at 21:39

2 Answers2

2

It doesn't matter about the height. But what does matter is that you need to pin it to the bottom of the scroll view also.

By pinning it on the top and bottom it will use the label to set the content size and so allow it to scroll.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
0

I suggest to add a UITableView instead of UIScrollView, adding one UITableViewcell that contains a UILabel. By setting the appropriate values of:

  • Label's constraints.
  • tableView's rowHeight.
  • tableView's estimatedRowHeight.

It should works fine for your case.

For more Information about setting a dynamic cell height, you might want to check this answer.

Hope that helped.

Community
  • 1
  • 1
Ahmad F
  • 30,560
  • 17
  • 97
  • 143