Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa Autolayout: content hugging vs content compression resistance priority

I can't find a clear answer on Apple documentation regarding Cocoa Autolayout about the difference between content hugging and compression resistance.

Can somebody explain their usages and difference ?

like image 395
dmitrynikolaev Avatar asked Apr 06 '13 11:04

dmitrynikolaev


People also ask

What is content hugging and content compression resistance priority?

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 priority in Autolayout IOS?

The priority really come in to play only if two different constraints conflict. The system will give importance to the one with higher priority. So, Priority is the tie-breaker in the autolayout world.

What is priority in constraints?

Ans : Constant priority is a number to determine how important is that constraint. The number can range from 1 to 1000, the higher the number goes, the more important a constraint is. Lower priority in screen seen like dashed blue line. This is useful when two constraint make conflict.


1 Answers

A quick summary of the concepts:

  • Hugging => content does not want to grow
  • Compression Resistance => content does not want to shrink

Example:

Say you've got a button like this:

[       Click Me      ] 

and you've pinned the edges to a larger superview with priority 500.

Then, if Hugging priority > 500 it'll look like this:

[Click Me] 

If Hugging priority < 500 it'll look like this:

[       Click Me      ] 

If the superview now shrinks then, if the Compression Resistance priority > 500, it'll look like this

[Click Me] 

Else if Compression Resistance priority < 500, it could look like this:

[Cli..] 

If it doesn't work like this then you've probably got some other constraints going on that are messing up your good work!

E.g. you could have it pinned to the superview with priority 1000. Or you could have a width priority. If so, this can be helpful:

Editor > Size to Fit Content

like image 154
Snowcrash Avatar answered Oct 06 '22 00:10

Snowcrash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!