If I throw together scenes consisting of either of the following two view hierarchies and constraints (easily done in InterfaceBuilder), then everything works as expected...
1)
- A full-screen
UIScrollView
(0 distance to all edges of the superview) - Within the
UIScrollView
, a singleUILabel
, withnumberOfLines
set to0
, with 100% width (0 distance leading and trailing constraints), with no height constraint, with enormous amounts of text (1000s of lines), and with top and bottom constraints with a constant of 0 (so that there should be 0 distance from the boundaries of theUILabel
to the content view of theUIScrollView
).
Behaviour is as expected: the UILabel
gets sized to fit its contents, which forces the content view of the UIScrollView
to expand to fit the text, making all the text scrollable.
2)
- A
UILabel
withnumberOfLines
equal to0
within someUIView
innerView within someUIView
outerView such that:- The label's edges all have 0 distance from innerView's edges (with priority 1000)
- innerView's edges all have 0 distance from outerView's edges (with priority 1000)
- outerView's top, leading and trailing edges all have 0 distance from the superview (with priority 1000)
- outerView has a height that is less than the full height of its superview, enforced either by a height constraint or a bottom-distance-to-superview constraint, but this final constraint has some lower priority - say, 600.
In this case the behaviour is again as expected: if the UILabel
's Content Compression Resistance Priority is greater than 600, outerView's height gets increased to fit all the text, and if it's less, the label gets truncated.
However the following case does not work the way I would expect, despite appearing to me to be exactly analogous to the other two:
3)
- A full-screen
UIScrollView
, like the first case - Within the
UIScrollView
, aUIView
innerView whose edges all have 0 distance from theUIScrollView
's edges (like the second case). - Within innerView, a
UILabel
withnumberOfLines
set to0
, loads of text, and Content Compression Resistance Priority set to 1000.
My expectation, based upon the previous two test cases, is that the UIScrollView
's content view should resize to fit the text of the label. However, instead what happens is that the content view gets set to the size of the UIScrollView
, as does innerView, and the UIScrollView
is not scrollable.
Why am I getting this behaviour which is inconsistent with my previous two test cases, and how can I get working content compression resistance and have the intrinsic content size of my UILabel
respected when I'm nesting it in a view within a UIScrollView
under autolayout? I can't see any reason for autolayout to compress my UILabel
in the third case when it is perfectly possible for it to not compress it and still satisfy all constraints in exactly the same way as it did in the first case.