本文翻译自: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文档 :

Cocoa Autolayout:内容拥抱与内容压缩阻力优先相关推荐

  1. linux命令看文件内容,Linux文件内容查看相关命令

    1.more命令 在Linux中,more命令是一个基于vi编辑器的文本过滤器,它能以全屏的方式按页显示文本文件的内容,more里面内置了一些快捷键. (1)命令语法 more(选项)(参数) (2) ...

  2. 互联网时代,一切商业皆内容,一切内容皆 IP

    讨论了关于互联网时代的内容营销的话题,内容营销必须有着深刻的战略思考和商业模式支撑. 文章转载于:http://36kr.com/p/5051972.html 场景实验室创始人吴声:一切商业皆内容,一 ...

  3. 内容理解、内容生成、内容推荐分发,在广告场景下的实践和探索(京东张政)

    内容理解.内容生成.内容推荐分发,在广告场景下的实践和探索(京东张政) 提示:广告也好,商品也罢,内容们,需要精准地推荐给用户,使其点击观看或者够买啥的,都需要走通一个重要流程:内容理解与推荐分发,更 ...

  4. 用python修改文件内容修改txt内容的3种方法

    用python修改文件内容修改txt内容的3种方法 方法一.修改原文件方式 def updateFile(file,old_str,new_str):"""替换文件中的字 ...

  5. java匹配出某单词除外_java正则匹配 指定内容以外的 内容

    今天,遇到一个需要 匹配出 指定内容以外的 内容的需求. 乍一看,需求貌视很简单啊,直接上 非贪婪模式的 双向零宽断言(有的资料上也叫 预搜索.预查.环视lookaround): 比如,我要匹配 串内 ...

  6. 用python处理excel的基本语法_《使用python3读取处理excel表的数据内容如何对内容求平均值》 用python读取excel文件...

    使用python3读取处理excel表的数据内容如何对内容求平均值 先说下概述: 平就合平均数的值. (a1 a2 --an)/na1,a2,--,an的平均值. 简单算均数.么一组数字10.20.3 ...

  7. css阻止换行_CSS中,如何处理短内容和长内容?

    本文已经过原作者 shadeed 授权翻译. 当我们使用 CSS 构建布局时,考虑长短文本内容很重要,如果能清楚地知道当文本长度变化时需要怎么处理,可以避免很多不必要的问题. 在许多情况下,添加或删除 ...

  8. html帧内容,html将内容从一个帧移动到另一个帧

    所以我把一个本地运行的网页放在一起,目前有2个框架 - 一个是导航栏,另一个是内容,用导航栏中的链接定义.这是在Internet Explorer 8中运行的,不幸的是必须保持这种状态. 从长远来看, ...

  9. php文章远程图片,php实现异步将远程链接上内容(图片或内容)写到本地的方法

    本文实例讲述了php实现异步将远程链接上内容(图片或内容)写到本地的方法.分享给大家供大家参考,具体如下: /** * 异步将远程链接上的内容(图片或内容)写到本地 * * @param unknow ...

最新文章

  1. C# try与finally(WinForm、Asp.Net)
  2. python语言入门自学-自学编程的6种方法,自学Python
  3. Spring Cloud Gateway 之获取请求体的几种方式
  4. 微信小程序项目实战知识点总结(swiper组件自适应高度,自定义弹出层,悬浮按钮,虚拟键盘)...
  5. [原创]Scala学习:编写Scala脚本
  6. 基于WF设计业务流程平台_权限在流程模板外部映射
  7. Android 监听开机完成广播,完成监听SIM卡功能
  8. 计算机专业复试有英语口语吗,计算机考研复试英语口语技巧
  9. 【Python刷题篇】——Python入门 01 输入输出
  10. 百度移动搜索落地页体验广告白皮书3.0上线
  11. QML 语法(Syntax)
  12. 【数据分析】京东2019校招数据分析工程师笔试题
  13. 基于java的学生学籍管理系统(含源文件)
  14. SEE MIPS RUN 第六章 内存管理与TLB
  15. 发版流程及对外版本规范
  16. 【wanaflyCamp】电音之王
  17. IDEA+Java+Servlet+JSP+Mysql实现学生选课签到系统
  18. linux中execve的用法,Linux 的 execve 函数
  19. 【驱动开发】Windows过滤平台(WFP,Windows Filtering Platform)
  20. 文职辅警的技能测试计算机操作是考什么,辅警文职体测都考哪些内容?

热门文章

  1. 15天玩转redis —— 第六篇 有序集合类型
  2. FOSRestBundle功能包:视图层
  3. Docker Compose安装以及入门
  4. 什么是.NET Framework
  5. cocos2dx 物理碰撞
  6. Best Coder Round#25 1003 树的非递归访问
  7. php截取等长UFT8中英文混合字串
  8. windows系统开机自动扫描硬盘的原因及解决方法
  9. win10 office 家庭和学生版 excel个人工作簿 PERSONAL.XLSB 的保存位置
  10. 2012年3月份工作总结 ~ 之 ~ PDF 作业对应 (虽然这个作业没有什么意思,但是非常值得总结)