在嵌套视图拥抱和COM pression阻力 [英] hugging and compression resistance in nested views

查看:120
本文介绍了在嵌套视图拥抱和COM pression阻力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何拥抱和COM pression性真正发挥作用。

I'm trying to understand how hugging and compression resistance really work.

我有这样的场景:我需要在右边左边两个标签(绿色的容器内)和两个标签(蓝色的容器内)。

I have this scenario where I need two labels on the left (inside the green container) and two labels on the right (inside the blue container).

在这里输入的形象描述

作为形象示人,我想绿色的容器拥抱内容(Android的包装内容)和蓝色的容器,以填补剩余的空间(Android的 FILL_PARENT )。

As the image shows, I want the green container to hug content (Android's wrap content) and the blue container to fill the remaining space (Android's fill_parent).

我想我可以只添加拥抱/ COM pression优先的绿色观点,如:

I thought I could just add hugging/compression priorities to the green view, like:

greenView.setContentHuggingPriority(
    UILayoutPriorityDefaultHigh, forAxis: .Horizontal)
greenView.setContentCompressionResistancePriority(
    UILayoutPriorityDefaultHigh, forAxis: .Horizontal)

但似乎预期这是行不通的。我有这些限制适用于(红色和黄色),而不是标签

But it seems that it doesn't work as expected. I have to apply those constraints to the (red and yellow) labels instead.

任何人都知道的原因?

的几点思考(编辑):

从肯的回答可以得出,你必须拥抱/ COM pression设置标签,而不是容器的看法。

From Ken's answer it follows that you have to set hugging/compression to the labels instead of container views.

在这个问题的例子中,我会设定为例如,750紧靠(高)和1000(必需)的电阻,以左侧的标签。因为缺省值标签拥抱251(低+ 1)和750(高)的电阻,拥抱和玉米pression将更大左侧(750> 251 1000> 750)的标签。同时,在COM pression会比非常标签(1000> 750)内的拥抱越大。

In the example of this question, I would set, for example, a hugging of 750 (High) and a resistance of 1000 (Required) to the labels on the left. Since defaults for labels are hugging of 251 (Low+1) and resistance of 750 (High), the hugging and compression will be greater for the labels on the left (750 > 251 and 1000 > 750). At the same time, the compression will be greater than the hugging within the very labels (1000 > 750).

这样,左边的标签将尝试拥抱自己的内容,但并不像对COM preSS它。例如,完全是因为黄色标签不希望COM preSS红色标签不能换行的内容。

This way, the labels on the left will try to hug their content, but not as much as to compress it. For example, the red label can't wrap its content completely because the yellow label doesn't want to compress.

唷!

推荐答案

内容拥抱和COM pression性的优先级相对于视图的固有内容尺寸仅为才有意义。基本上,如果一个视图具有固有的内容宽度,然后自动布局系统把它当作好像它是受到以下限制:

Content hugging and compression resistance priorities only have meaning in relation to the view's intrinsic content size. Basically, if a view has an intrinsic content width, then the auto layout system treats it as if it were subject to the following constraints:

[view(<=intrinsicWidth@contentHuggingPriority)]
[view(>=intrinsicWidth@compressionResistancePriority)]

这是所有这些的意思。这同样适用于本征的高度,也当然

That's all those mean. The same applies to the intrinsic height, too, of course.

作为容器使用一个普通的的UIView 没有内在的大小。因此,其内容拥抱和COM pression性的优先级是毫无意义的。

A plain UIView used as a container has no intrinsic size. So, its content hugging and compression resistance priorities are meaningless.

这篇关于在嵌套视图拥抱和COM pression阻力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆