ios UIView 的内容压缩阻力和内容拥抱是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22599069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:47:49  来源:igfitidea点击:

What is the content compression resistance and content hugging of a UIView?

ioscocoa-touchuiviewautolayout

提问by smileyborg

What is the content compression resistance and content hugging of a UIView? How do these relate to the intrinsic content size of a view?

UIView 的内容压缩阻力和内容拥抱是什么?这些与视图的内在内容大小有何关系?

回答by smileyborg

Taken from objc.io's excellent Advanced Auto Layout Toolbox article:

摘自objc.io优秀的Advanced Auto Layout Toolbox 文章

Intrinsic Content Size

The intrinsic content size is the size a view prefers to have for a specific content it displays. For example, UILabel has a preferred height based on the font, and a preferred width based on the font and the text it displays. A UIProgressView only has a preferred height based on its artwork, but no preferred width. A plain UIView has neither a preferred width nor a preferred height.

Compression Resistance and Content Hugging

Each view has content compression resistance priorities and content hugging priorities assigned for both dimensions. These properties only take effect for views which define an intrinsic content size, otherwise there is no content size defined that could resist compression or be hugged.

Behind the scenes, the intrinsic content size and these priority values get translated into constraints. For a label with an intrinsic content size of { 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)]

If you're not familiar with the visual format language for the constraints used above, you can read up about it in Apple's documentation. Keeping in mind that these additional constraints are generated implicitly helps to understand Auto Layout's behavior and to make better sense of its error messages.

内在内容大小

内在内容大小是视图对于它显示的特定内容的首选大小。例如, UILabel 有一个基于字体的首选高度,以及一个基于字体和它显示的文本的首选宽度。UIProgressView 只有基于其艺术作品的首选高度,但没有首选宽度。一个普通的 UIView 既没有首选宽度也没有首选高度。

抗压性和内容拥抱

每个视图具有为两个维度分配的内容压缩抵抗优先级和内容拥抱优先级。这些属性仅对定义了内在内容大小的视图有效,否则没有定义可以抵抗压缩或被拥抱的内容大小。

在幕后,内在内容大小和这些优先级值被转化为约束。对于内在内容大小为 {100, 30},水平/垂直压缩阻力优先级为 750,水平/垂直内容拥抱优先级为 250 的标签,将生成四个约束:

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

如果您不熟悉上面使用的约束的视觉格式语言,您可以在 Apple 的文档中阅读它。请记住,这些附加约束是隐式生成的,有助于理解自动布局的行为并更好地理解其错误消息。

Here's another StackOverflow question that addresses the difference between content compression resistance & content hugging: Cocoa Autolayout: content hugging vs content compression resistance priority

这是另一个 StackOverflow 问题,它解决了内容压缩阻力和内容拥抱之间的区别: Cocoa Autolayout:内容拥抱与内容压缩阻力优先级