How to resize an NSImageView in an NSStackView?

188 Views Asked by At

I'm working on a project where I want the following in an NSTableViewCell:

  • Image

  • Text

  • Subtitle text

The NSTableView is in a window which the user can expand or contract. When the window expands and contracts, the text wraps as needed.

That much is working.

When it comes to the image view, I can't get the thing to resize at all. I don't understand how the text automatically wraps, but images don't automatically scale. I've been working on iOS so long that I might have missed something in how stack views differ between iOS and macOS, but I never had this problem in iOS.

I don't have much code because the text wraps properly without any code at all, so instead I posted a minimal project showing the problem on Github:

NSImageTableViewTest

Some things I tried:

I have to set the width/height of the image view, or the text won't wrap. It seems to me that the reason why is that if I keep the image view unbound, the table view starts at the width of the image.

I tried setting the leading and bottom constraints of the image view to no avail.

I tried setting the constraints of the NSStackView, but that doesn't help constrain the frame of the image view.

Question: do I have to change the frame of the image view in code? I did try that, to no avail.

At this point, I'm stumped and I'm sure the fix is something easy that I overlooked.

Thanks.

1

There are 1 best solutions below

0
Willeke On BEST ANSWER

The image view doesn't shrink because the default Content Compression Resistance Priority is too high. Set the Content Compression Resistance Priority to (a bit lower than) "Low (250)".