Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the content compression resistance and content hugging of a UIView?

What is the content compression resistance and content hugging of a UIView? How do these relate to the intrinsic content size of a view?

like image 766
smileyborg Avatar asked Mar 24 '14 00:03

smileyborg


People also ask

What is content hugging and compression resistance?

The content-hugging priority represents the resistance a view has to grow larger than its intrinsic content size. Conversely, the compression-resistance priority represents the resistance a view has to shrink beyond its intrinsic content size.

What is intrinsic content size Swift?

Most views have an intrinsic content size, which refers to the amount of space the view needs for its content to appear in an ideal state. For example, the intrinsic content size of a UILabel will be the size of the text it contains using whatever font you have configured it to use.

What is priority in storyboard?

When you define a new constraint in Interface Builder / Storyboard, Xcode will auto assign the constraint priority as 1000. 1000 is the highest constraint priority, meaning it is mandatory as no other constraint can have higher priority than this and this constraint will never be ignored in favor of others.


1 Answers

Taken from objc.io's excellent Advanced Auto Layout Toolbox article:

Intrinsic Content Size

The intrinsic content size is the size a view prefers to have for a specific content it displays. For example, UILabel has a preferred height based on the font, and a preferred width based on the font and the text it displays. A UIProgressView only has a preferred height based on its artwork, but no preferred width. A plain UIView has neither a preferred width nor a preferred height.

Compression Resistance and Content Hugging

Each view has content compression resistance priorities and content hugging priorities assigned for both dimensions. These properties only take effect for views which define an intrinsic content size, otherwise there is no content size defined that could resist compression or be hugged.

Behind the scenes, the intrinsic content size and these priority values get translated into constraints. For a label with an intrinsic content size of { 100, 30 }, horizontal/vertical compression resistance priority of 750, and horizontal/vertical content hugging priority of 250, four constraints will be generated:

H:[label(<=100@250)] 
H:[label(>=100@750)]
V:[label(<=30@250)]
V:[label(>=30@750)]

If you’re not familiar with the visual format language for the constraints used above, you can read up about it in Apple’s documentation. Keeping in mind that these additional constraints are generated implicitly helps to understand Auto Layout’s behavior and to make better sense of its error messages.

Here's another StackOverflow question that addresses the difference between content compression resistance & content hugging: Cocoa Autolayout: content hugging vs content compression resistance priority

like image 110
smileyborg Avatar answered Oct 08 '22 20:10

smileyborg