Content Hugging & Content Compression Resistance Priority [iOS — CHCR priorities]
In Auto layout, sometimes you need to use Content Hugging & Compression Resistance(CHCR) priorities to determine the best layout for views that are encountering constraints that would require them to be larger/smaller than their given default size. So here i’m summarizing the differences between two and when is the right time to use both of the priorities.
Before getting to know two priorities, we first need to understand the meaning of intrinsicContentSize.
The natural size for the receiving view, considering only properties of the view itself which is a get-only property. For example UILable, UIButton, UISwitch and including other objects can have width and the height of itself(given by system) and what we called an intrinsicContentSize.
Content Hugging priority
Returns the priority with which a view resists being made larger than its intrinsic size. Hugging is usually used when the content does not want to grow and usually has enough space. So using hugging priority, you can define which object should have higher priority to expand or shrink.
So if the view has higher hugging priority then the object will shrink because it can only have less than or equal to its size.
For example, if there’s two labels as above and both constraints set to 20 for the top, left, right. then you’ll encounter an error which says that set horizontal hugging priority because XCode doesn’t know which UILabel to grow as the constraints are given. That’s why we have to set a hugging priority to expand the size when there’s enough space for the objects.
So let’s see what will happen if i set the priority greater than other. I’ll set the pink background’s label’s priority to 252 then you will see the result in the image below.
The UILabel which has the greater priority still keeps its size and the other object expands its size within its constraints.
I personally think it’s weird that the higher priority gets the smaller space which does not grow. but that’s what hugging means in Xcode. So In other words, the pink view can have less width than itself. That’s why the yellow view has to have the bigger value of the width.
Content Compression Resistance Priority
Returns the priority with which a view resists being made smaller than its intrinsic size. Compression resistance priority is usually used when the content does not want to shrink, like when there’s not enough space for views to fit. Higher priority should always be greater than its size.
so if there’s not enough space between the objects and to make one’s priority to show the whole text, then the higher priority keeps its size and which has less priority will shrink within its constraints.
So if I make the label with the pink color’s compression priority greater than the yellow label, it will look like below. You will see the yellow color label shrinks and the pink color label keeps its own size.
When it’s best to use Both Priorities?
- Content Hugging Priority: When there’s enough space or empty space
- Compression Resistance: When there’s not enough space
Example of using both priorities
There’s so many ways to use both priorities. when there’s more than two objects that might have a possibility to overwrap each other, then set a CHCR priority to make the objects more visible than others.