Wednesday, July 9, 2014

Auto Layout and Views That Wrap

Jonathon Mah:

The first call to [super layoutSubviews] will evaluate the constraints on the label (since it’s a direct subview) and change its frame accordingly. At this point the width is useful, but the height is not; the height was set using the label’s intrinsic content size, which in turn relied on a preferred max layout width value that is now stale.

Now we know the actual width of the label, we set that as its max layout width. Internally, this causes the label to invalidate its intrinsic content size; when it’s next queried, it will have the accurate height for its current width. With all layout information in place, we call [super layoutSubviews] again.

Comments RSS · Twitter

Leave a Comment