Content Hugging and Compression Resistance Explained

In a given view, there may be extra space horizontally and not enough vertically, or vice versa(或者相反). There may also be extra or not enough in both directions.

Suppose extra space is available for a view. Which control(控件)(subview) should grow to take the extra space? For that control, set hugging(紧抱)priority lower, and you are telling Auto Layout not to hug that control. In other words, that is the control you would like to grow(拉伸),(hugging priority:吸附能力、抗拉伸的权重)

Lower hugging priority means grow bigger. Higher hugging priority means stay(保持) the same and resist(抵抗) getting bigger

Suppose there is not enough space for a view. Which control (subview) should allow itself to be shrunk(压缩) or compressed? For that control(控件), set compression resistance priority lower, and you are telling Auto Layout that it can shrink this control because it is the one with the least resistance to being compressed.

Lower compression resistance means compress and make smaller. Higher compression resistance means stay the same and resist getting smaller

These are the tenets(原则) of Content Hugging Priority and Compression Resistance Priority. Auto Layout needs you to tell it how to handle cases of more or less space, otherwise the results may be unexpected and undesirable(不如所料的) when the size of a view changes or doesn’t match intrinsic content sizes.

Many controls have an intrinsic content size, which means the size it would like to be in order to accommodate (适应 )its content. For some controls, such as UIView, there is no intrinsic content size. Some controls have both a height and width intrinsic content size. Controls with both height and width intrinsic content size include UILabel and UIButton. 在XIB中UIImage在没有设置图片的时候没有intrinsic content,设置图片后就有了.

In general, controls have a default value of 250 for horizontal and vertical Content Hugging Priority, and a default value of 750 for horizontal and vertical Compression Resistance Priority. More about that shortly, but based on those defaults it is therefore easier to stretch(拉伸) than it is to shrink.

如果错误欢迎指正
原文链接:http://www.iosinsight.com/con…

    原文作者:Stephanie
    原文地址: https://segmentfault.com/a/1190000009611519
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞