Mastering Content Hugging and Content Compression Resistance Priorities: Achieving Responsive UIs in Auto Layout

Abdul Karim Khan
4 min readAug 7, 2023

--

In Auto Layout, Content Compression Resistance and Content Hugging Priority are fundamental concepts that play a crucial role in determining how views respond to dynamic content and available space. By understanding these properties, you gain precise control over how your user interface adapts to different screen sizes, orientations, and content.

The priority comes into action when we get constraints conflict issues. The system will give priority to one with higher value. In the realm of auto-layout, priority is the deciding factor.

Quick link to tutorial :)

Before diving into priorities, let’s take a quick look at the image below in order to learn more efficiently.

An apple is being compressed by two individuals.

Let’s consider an apple as our view, which is being compressed by other views. Apple has some resistance power that is stoping itself from being compressed which is compression resistance priority where as individuals are applying force by hugging it.

Content Compression Resistance Priority (CCR)

Content Compression Resistance Priority (CCR) is a value that determines how strongly a view resists being compressed when there is insufficient space to display its content fully. It prevents the content from being clipped or truncated, ensuring that essential information remains visible.

Consider a label containing a lengthy text that may not fit within the available width. By setting a higher CCR priority, you prioritise the preservation of the label’s content, making the label less likely to compress. Instead, other flexible elements in the layout may adjust their sizes to accommodate the label’s content.

Key Points:

  • Higher CCR priority prevents content compression.
  • Views with higher CCR prioritise their content over other flexible elements.
  • Useful for preserving essential information in dynamically changing layouts.

Example: Let’s create a button with large title.
Problem:
Button will not expand as per its content inside automatically.

Solution: We need to expand this button as per content inside. Compression Resistance Priority comes into action and by increasing its priority to 1000 we can solve this problem. But how?

  • We will update width constraint priority to 999.
  • We will increase button’s compression resistance priority to 1000.
  • As a result, auto layout will let button to take width as per content inside.

Higher CCR priority prevents content compression.

Content Hugging Priority (CHP)

Content Hugging Priority (CHP) is a value that determines how strongly a view clings to its intrinsic content size, preferring not to expand beyond it when there is additional space available.

Imagine a label with a short text inside a container. By setting a higher CHP priority for the label, you ensure that it does not stretch to fill extra space, leaving the rest of the layout more flexible and accommodating.

Key Points:

  • Higher CHP priority prevents the view from expanding beyond its intrinsic content size.
  • Views with higher CHP prioritise staying at their natural size over occupying additional space.
  • Useful for maintaining balanced and aesthetically pleasing layouts.

Example: Two labels placed next to each other.
Problem:
Two labels are placed next to each other with centre vertically and leading, trailing constraints. Xcode can’t determine the width of these labels, therefore priority is required.

By default horizontal hugging priority of labels are 251. Label with higher horizontal hugging priority will not grow more than its size.

Solution:

  • We will increase horizontal hugging priority of mint label to 252.
  • We will keep green label priority to 251.
  • As a result, width of green label will increase and mint one will maintain its intrinsic content size.

Hence, label or any view with higher hugging priority will stick to its intrinsic content size.

Thanks!

WATCH TUTORIAL HERE!!

https://youtu.be/tEsri8P9qQ8?si=1A9SLuXHhs13Ym7n

--

--