Auto-Layout Question


Rick Aurbach
 

I would like some help building constraints that adjust a superView to the size of it's subView.

Specifically, I have a UILabel embedded in a UIView. The label has fixed width and (since numberOfLines = 0) a height which depends on the label's content. I want to use auto-layout to auto-size the exterior view so that there is a 5 pixel margin around the UILabel.

Suggestions?

Thanks,

Rick Aurbach


Steve Christensen
 

Bump up the label's content hugging and compression resistance values above the normal/standard values, at least above the values used for "don't particularly care how big they are" views. The default settings give all views an equal shot to fit together so this prioritizes the label's size. Then set the top, bottom, leading, and trailing edge offsets between the label and its enclosing view.

Assuming nothing else in your overall view hierarchy gets in the way, autolayout will base the enclosing view's size on the label's size.

On Dec 22, 2017, at 10:04 AM, Rick Aurbach <rlaurb@me.com> wrote:

I would like some help building constraints that adjust a superView to the size of it's subView.

Specifically, I have a UILabel embedded in a UIView. The label has fixed width and (since numberOfLines = 0) a height which depends on the label's content. I want to use auto-layout to auto-size the exterior view so that there is a 5 pixel margin around the UILabel.

Suggestions?

Thanks,

Rick Aurbach