aneuryzm
aneuryzm

Reputation: 64834

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?

Upvotes: 2

Views: 206

Answers (1)

DonMag
DonMag

Reputation: 77442

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. But once you set its .image property, its intrinsic content size will be the size of the 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. 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.


EDIT

Here is a (rather long) example:

Here I have a UIView (green) containing a UIStackView set to Fill / Fill / 8. The stack view holds a UIImageView (Scale To Fill) and a UILabel. The image view has NO intrinsic content size, while the label has an intrinsic content size, based on its content.

enter image description here

With the constraints I've set, the stack view is 200 x 240 points.

At run-time, I'm going to set the image to a 200 x 100 png of a cat:

enter image description here

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). With both the image view and the label having Vertical Content Hugging: 251 (the default), this is the result:

enter image description here

But I want the image to retain its original aspect ratio of 2:1. First thought is to change the image view's Content Mode to Aspect Fit -- but I get this result:

enter image description here

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). The output:

enter image description here

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.

So, I give the image view a Placeholder intrinsic content size of 200 x 100, and I see this:

enter image description here

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.

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.

Upvotes: 1

Related Questions