简体   繁体   English

没有内在内容大小的 UIViews 的拥抱/压缩?

[英]Hugging/compression for UIViews without intrinsic content size?

I've been asked this question on a job interview and I'm wondering what he meant.我在求职面试中被问到这个问题,我想知道他的意思。

The role of hugging and compression properties for any view with intrinsic content size is obvious.对于具有内在内容大小的任何视图,拥抱和压缩属性的作用是显而易见的。

But what about views without intrinsic content size?但是没有内在内容大小的视图呢? Do they play a role there?他们在那里发挥作用吗?

Kind of an odd question, without some additional context.有点奇怪的问题,没有一些额外的背景。

However, I suppose you could discuss a couple things...但是,我想你可以讨论一些事情......

  1. Some views get an intrinsic content size after-the-fact.一些视图在事后获得了内在的内容大小。 A UIImageView , for example... without an image, it has no intrinsic content size.一个UIImageView ,例如...没有图像,它没有内在的内容大小。 But once you set its .image property, its intrinsic content size will be the size of the image.但是一旦你设置了它的.image属性,它的内在内容大小将是图像的大小。 So, you may want to set the priorities based on what will happen.因此,您可能希望根据将要发生的情况设置优先级。

  2. You can give a UIView , for example, "Placeholder" intrinsic content size.您可以提供UIView ,例如“占位符”内在内容大小。 Changing the hugging/compression priorities will affect that view during design-time.在设计时更改拥抱/压缩优先级影响该视图。

In both of those cases, you would be "planning ahead" during Storyboard / IB design for what you want at run-time.在这两种情况下,您都将在 Storyboard / IB 设计期间“提前计划”,以获得您在运行时想要的东西。


EDIT编辑

Here is a (rather long) example:这是一个(相当长的)示例:

Here I have a UIView (green) containing a UIStackView set to Fill / Fill / 8 .在这里,我有一个UIView (绿色),其中包含一个UIStackView设置为Fill / Fill / 8 The stack view holds a UIImageView ( Scale To Fill ) and a UILabel .堆栈视图包含一个UIImageViewScale To Fill )和一个UILabel The image view has NO intrinsic content size, while the label has an intrinsic content size, based on its content.图像视图没有固有内容大小,而标签具有基于其内容的固有内容大小。

在此处输入图片说明

With the constraints I've set, the stack view is 200 x 240 points.根据我设置的约束,堆栈视图为200 x 240点。

At run-time, I'm going to set the image to a 200 x 100 png of a cat:在运行时,我要将图像设置为200 x 100 png 的猫:

在此处输入图片说明

My goal is for the image view to "fit to the image," letting the label expand vertically.我的目标是让图像视图“适合图像”,让标签垂直扩展。

When I set the .image via code (at run-time), the image view "gets" an intrinsic content size (of 200 x 100 in this case).当我通过代码(在运行时)设置.image时,图像视图“获得”一个内在内容大小(在这种情况下为200 x 100 )。 With both the image view and the label having Vertical Content Hugging: 251 (the default), this is the result:图像视图和标签都具有Vertical Content Hugging: 251 (默认值),结果如下:

在此处输入图片说明

But I want the image to retain its original aspect ratio of 2:1 .但我希望图像保留其原始纵横比2:1 First thought is to change the image view's Content Mode to Aspect Fit -- but I get this result:第一个想法是将图像视图的内容模式更改为Aspect Fit —— 但我得到了这个结果:

在此处输入图片说明

Now my image view is "letter-boxing" the image, showing the red background color.现在我的图像视图是“信箱”图像,显示红色背景色。

So, I change the image view's Vertical Content Hugging: 252 (higher priority than the label).因此,我更改了图像视图的Vertical Content Hugging: 252 (优先级高于标签)。 The output:输出:

在此处输入图片说明

Now I have the run-time output that I want, but at design-time (working in Storyboard / IB), nothing has changed and it may not really be clear what's going to happen.现在我有了我想要的运行时输出,但在设计时(在 Storyboard / IB 中工作),没有任何改变,可能不清楚会发生什么。

So, I give the image view a Placeholder intrinsic content size of 200 x 100 , and I see this:所以,我给图像视图一个200 x 100占位符内在内容大小,我看到了这个:

在此处输入图片说明

Because it's a placeholder , that will have no effect on the run-time output (if I set the image to a 200 x 50 image it will be 200 x 50 at run-time), but it better reflects what I'm expecting.因为它是一个占位符,所以不会对运行时输出产生影响(如果我将图像设置为200 x 50图像,它在运行时将为200 x 50 ),但它更好地反映了我的期望。

Of course, there are other ways to accomplish the goal, and this may not be the ideal approach, but it (hopefully) gives an idea of one way that setting Hugging / Compression priorities on elements with no intrinsic content size can be useful / needed.当然,还有其他方法可以实现目标,这可能不是理想的方法,但它(希望)提供了一种方法,即在没有内在内容大小的元素上设置拥抱/压缩优先级可能有用/需要.

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

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