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 ?
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.
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.
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.
A quick summary of the concepts:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With