Cocoa Autolayout:内容拥抱与内容压缩阻力优先

本文翻译自: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. 关于Cocoa Autolayout关于内容拥抱和抗压缩性之间差异的Apple文档,我找不到明确的答案。

Can somebody explain their usages and difference ? 有人可以解释他们的用法和差异吗?


#1楼

参考:https://stackoom.com/question/14VPt/Cocoa-Autolayout-内容拥抱与内容压缩阻力优先


#2楼

Let's say you have a button with the text, "Click Me". 假设您有一个带有“Click Me”文本的按钮。 What width should that button be? 该按钮的宽度应该是多少?

First, you definitely don't want the button to be smaller than the text. 首先,你绝对不希望按钮小于文本。 Otherwise, the text would be clipped. 否则,文本将被剪裁。 This is the horizontal compression resistance priority. 这是水平压缩阻力优先级。

Second, you don't want the button to be bigger than it needs to be. 其次,你不希望按钮大于它需要的大。 A button that looked like this, [ Click Me ], is obviously too big. 一个看起来像这样的按钮,[Click Me],显然太大了。 You want the button to "hug" its contents without too much padding. 你希望按钮“拥抱”其内容而不需要太多填充。 This is the horizontal content hugging priority. 这是水平内容拥抱优先级。 For a button, it isn't as strong as the horizontal compression resistance priority. 对于按钮,它不如水平压缩阻力优先级强。


#3楼

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. 并且你已经将边缘固定到一个优先级为500的更大的超级视图。

Then, if Hugging priority > 500 it'll look like this: 然后,如果拥抱优先级> 500,它将如下所示:

[Click Me]

If Hugging priority < 500 it'll look like this: 如果拥抱优先级<500,它将如下所示:

[       Click Me      ]

If the superview now shrinks then, if the Compression Resistance priority > 500, it'll look like this 如果superview现在收缩,如果压缩阻力优先级> 500,它将如下所示

[Click Me]

Else if Compression Resistance priority < 500, it could look like this: 否则,如果压缩电阻优先级<500,它可能如下所示:

[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! 如果它不能像这样工作那么你可能还有一些其他的限制正在弄乱你的好工作!

Eg you could have it pinned to the superview with priority 1000. Or you could have a width priority. 例如,你可以将它固定到优先级为1000的superview。或者你可以拥有宽度优先级。 If so, this can be helpful: 如果是这样,这可能会有所帮助:

Editor > Size to Fit Content 编辑器>适合内容的大小


#4楼

Take a look at this video tutorial about Autolayout , they explain it carefully 看看这个关于Autolayout的视频教程 ,他们会仔细解释

在此输入图像描述


#5楼

If view.intrinsicContentSize.width != NSViewNoIntrinsicMetric , then auto layout creates a special constraint of type NSContentSizeLayoutConstraint . 如果view.intrinsicContentSize.width != NSViewNoIntrinsicMetric ,则自动布局会创建NSContentSizeLayoutConstraint类型的特殊约束。 This constraint acts like two normal constraints: 此约束的作用类似于两个常规约束:

  • a constraint requiring view.width <= view.intrinsicContentSize.width with the horizontal hugging priority, and 需要view.width <= view.intrinsicContentSize.width且具有水平拥抱优先级的约束,和
  • a constraint requiring view.width >= view.intrinsicContentSize.width with the horizontal compression resistance priority. 需要view.width >= view.intrinsicContentSize.width且具有水平压缩阻力优先级的约束。

In Swift, with iOS 9's new layout anchors, you could set up equivalent constraints like this: 在Swift中,使用iOS 9的新布局锚点,您可以设置如下的等效约束:

let horizontalHugging = view.widthAnchor.constraint(
    lessThanOrEqualToConstant: view.intrinsicContentSize.width)
horizontalHugging.priority = view.contentHuggingPriority(for: .horizontal)

let horizontalCompression = view.widthAnchor.constraint(
    greaterThanOrEqualToConstant: view.intrinsicContentSize.width)
horizontalCompression.priority = view.contentCompressionResistancePriority(for: .horizontal)

Similarly, if view.intrinsicContentSize.height != NSViewNoIntrinsicMetric , then auto layout creates an NSContentSizeLayoutConstraint that acts like two constraints on the view's height. 类似地,如果view.intrinsicContentSize.height != NSViewNoIntrinsicMetric ,则自动布局会创建一个NSContentSizeLayoutConstraint ,其作用类似于视图高度的两个约束。 In code, they would look like this: 在代码中,它们看起来像这样:

let verticalHugging = view.heightAnchor.constraint(
    lessThanOrEqualToConstant: view.intrinsicContentSize.height)
verticalHugging.priority = view.contentHuggingPriority(for: .vertical)

let verticalCompression = view.heightAnchor.constraint(
    greaterThanOrEqualToConstant: view.intrinsicContentSize.height)
verticalCompression.priority = view.contentCompressionResistancePriority(for: .vertical)

You can see these special NSContentSizeLayoutConstraint instances (if they exist) by printing view.constraints after layout has run. 您可以通过在布局运行后打印view.constraints来查看这些特殊的NSContentSizeLayoutConstraint实例(如果存在)。 Example: 例:

label.constraints.forEach { print($0) }

// Output:
<NSContentSizeLayoutConstraint:0x7fd82982af90 H:[UILabel:0x7fd82980e5e0'Hello'(39)] Hug:250 CompressionResistance:750>
<NSContentSizeLayoutConstraint:0x7fd82982b4f0 V:[UILabel:0x7fd82980e5e0'Hello'(21)] Hug:250 CompressionResistance:750>

#6楼

Content Hugging and Content Compression Resistence Priorities work for elements which can calculate their size intrinsically depending upon the contents which are coming in. 内容拥抱和内容压缩阻力优先级适用于可以根据正在进入的内容本质地计算其大小的元素。

From Apple docs : 来自Apple文档

在此输入图像描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值