0

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.

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
Michael Gaines
  • 383
  • 3
  • 13
  • When and how do you want the image to scale? Does the image in the view not scale or does the image view not resize? – Willeke Jan 19 '23 at 07:06
  • I'd like the image to scale when the table view grows and shrinks when the window size changes. The image view doesn't scale. – Michael Gaines Jan 19 '23 at 15:43
  • The image will automatically scale when the image view resizes if Scaling is set to Proportionally Up or Down. – Willeke Jan 19 '23 at 22:22
  • I tried that. Didn't work. – Michael Gaines Jan 19 '23 at 22:45
  • How do you resize the image view? – Willeke Jan 20 '23 at 00:08
  • The whole thing is in the GitHub project. It's probably easier to take a look at it. – Michael Gaines Jan 20 '23 at 01:19
  • I did take a look at it but I don't understand what you're trying to accomplish. Are all images the same size? What should the relationship be between the scale of the image, the size of the image view and/or the size of the table view? – Willeke Jan 20 '23 at 11:15
  • I thought I made it very clear - I want the image to scale when the window size changes. "I don't understand how the text automatically wraps, but images don't automatically scale" – Michael Gaines Jan 20 '23 at 13:37
  • Images do automatically scale but the image view doesn't resize. The image view in your storyboard has fixed width and height constaints so it won't resize. The text field does automatically resize. – Willeke Jan 20 '23 at 14:54
  • Yes, I addressed that here: "I have to set the width/height of the image, or the text won't wrap. It seems to me that the reason why is that if I keep the image unbound, the table view starts at the width of the image.". If I remove the constraints, the image is shown in its original size, but doesn't scale. That's the problem. If you remove the constraints from the image, it won't scale. – Michael Gaines Jan 20 '23 at 15:29
  • What size should the image view be? Same width as the cell view? What height? Do you need the stack view? – Willeke Jan 20 '23 at 16:37
  • 1
    "I'm sure the fix is something easy that I overlooked": I think you missed the Content Compression Resistance Priority of the image view. – Willeke Jan 21 '23 at 15:52
  • That was it! Thanks so much! I was so focused on constraints that I overlooked this. @Willeke - can you post this an an answer please? – Michael Gaines Jan 23 '23 at 04:09

1 Answers1

1

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)".

Willeke
  • 14,578
  • 4
  • 19
  • 47