美文网首页
Content Hugging Priority and Con

Content Hugging Priority and Con

作者: 董二千 | 来源:发表于2016-01-20 20:11 被阅读39次

概念

Conent Hugging *
<pre>内容抱紧:在此基础上不再
变大</pre>
Content Compression
<pre>内容压缩:在此基础上不再
变小*</pre>

举个栗子

view上面放两个UILabel,两个Label水平,且上下左右都有约束。

  • First step 对这两个约束不做任何改动,如下设置
    <pre>self.leftLabel.text = @"aaaaaaaaaaaa";</br>self.leftLabel.textColor = [UIColor redColor];</br>self.rightLabel.text = @"bbbbb";</br>self.rightLabel.textColor = [UIColor greenColor];</pre>
左边是leftLabel、右边是rightLabel.png

可见:默认情况下,右边的都展示出来了,而左边的没有都展示出来。因为苹果为了保证约束,牺牲了Label内容的大小,用...来表示了

  • Second step 修改两个Label水平方向上的Content Compression的优先级,让leftLabel小于rightLabel,让rightLabel不被压缩
leftLabel content compression 约束的优先级小于 rightLabel.png

可见:rightLabel都展示出来了,而leftLabel的内容被牺牲了,满足了约束的需求。

Tip:其实还有种方法能满足上面的需求

rightLabel的右侧约束的优先级改成比content compression还低,苹果会牺牲该约束的长度,来满足需求。效果和上图一样。
size :{ 100, 30 }
horizontal/vertical compression resistance priority of 750
and horizontal/vertical content hugging priority of 250
four constraints will be generated:

H:[label(<=100@250)]
H:[label(>=100@750)]
V:[label(<=30@250)]
V:[label(>=30@750)]

也就是说compression最小不能小于某个值,hugging的最大不能大于某个值

相关文章

网友评论

      本文标题:Content Hugging Priority and Con

      本文链接:https://www.haomeiwen.com/subject/zgehhttx.html