DEV Community

Agoi Abel Adeyemi
Agoi Abel Adeyemi

Posted on

View Properties use by Auto Layout

Intrinsic content size, Compression Resistance Priority, Content Hugging Priority

There are some views that automatically size themselves base on the content they hold, This is referred to as their intrinsic content size. E.G A button’s intrinsic content size is the size of its title plus a small margin. Not all views have an intrinsic content size. For views that do, the intrinsic content size can define the view’s height, it’s width or both.

Let say we have a button with the text “Click Me”.

We definitely don’t want the button to be smaller than the text inside it, Otherwise the text would be clipped. This is know as Compression Resistance Priority.

The higher a view’s Compression Resistance Priority, the more the view resists growing larger than its intrinsic content size.

Still on the “Click Me” button, If we don’t want the button to be really bigger than the content inside it like the image below:

If we want the button to hug its content without too much padding, we have to increase the Content Hugging Priority.

The higher a view’s Content Hugging Priority is, the more the view resists growing larger than its intrinsic content size.

We can set the content hugging and compression resistance priority programmatically like below:

The two methods take two parameters which are the UILayoutPriority and the UILayoutConstraintAxis. The priority level can be required, defaultHigh, defaultLow, fittingSizeLevel where required is the highest and fittingSizeLevel is the lowest. The UILayoutConstraintAxis can either be a vertical or horizontal axis.

I will write another article on UIStackview, after which I will show ways to programmatically code different views. Thanks for reading.

Top comments (0)