简体   繁体   English

堆栈视图和内容拥抱优先级 - 意外行为

[英]Stack View & Content Hugging Priority - Unexpected Behaviour

I have inside a collection view cell a stack view with 3 elements (2 image views and 1 label), as shown in the following screenshot: 我在集合视图单元格内部有一个包含3个元素的堆栈视图(2个图像视图和1个标签),如下面的屏幕截图所示:

在此输入图像描述 What I'm trying to achieve, is to have the label taking all the possible empty space instead of image views, which is what is shown in the xib. 我想要实现的是让标签占用所有可能的空白空间而不是图像视图,这就是xib中显示的内容。 In theory, the following goal should be achieved simply having a bigger Content Hugging Priority value on the image views, since a high value implies that we don't want that a certain view to be larger then its content. 理论上,只需在图像视图上具有更大的Content Hugging Priority值就可以实现以下目标,因为高值意味着我们不希望某个视图比其内容大。

But the curious results that I get from various experiments are the following: 但是我从各种实验得到的好奇结果如下:

1 Not working as expected 1未按预期工作

Content hugging on ImageViews: 251 在ImageViews上拥抱的内容:251

Content hugging on Label: 250 标签上拥抱的内容:250

在此输入图像描述

2 OK 2好的

Content hugging on ImageViews: 999 在ImageViews上拥抱的内容:999

Content hugging on Label: 250 标签上拥抱的内容:250

在此输入图像描述

So, even if both cases should be working theoretically at the same way, (as in the interface builder) the truth is that at runtime they aren't! 因此,即使两种情况理论上都应该以相同的方式工作,(如在界面构建器中),事实是在运行时它们不是! Do you know why is this happening? 你知道为什么会这样吗? What did I miss? 我错过了什么? Thanks! 谢谢!

PS: The stack view has Distribution: fill and center alignment PS:堆栈视图具有Distribution: fill和中心对齐

Set a width & height constraints for the 2 images 设置2个图像的宽度和高度约束

在此输入图像描述

Also don't forget to implement sizeForItemAt 另外不要忘记实现sizeForItemAt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM