Tim
Tim

Reputation: 235

Resize image view to fit image, not other way around

I'm using XCode 6 to develop an application for OSX using Swift. In my window I have an image view which displays different images depending on which entry is selected in a table just above it. All of the images that can be displayed are the same width, but different height.

What I would like is for the image view (and the window!) to resize depending on which image is selected. I have no idea of how to do this in the Interface Builder (which is what I have used (bindings etc.) for most of my app), and can't find any tutorials/questions about it, since searching always gives results for how to resize images to fit an image view, not the other way around!

If you need any more specific information about my app, please just let me know what you need.

Edit: There are currently no constraints at all on anything in my window, but the only way I can see to edit the content hugging and compression resistance priorities is to have at least one constraint on the image view. Hence the top space constraint to the search bar above.

enter image description here

Upvotes: 0

Views: 2208

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90521

Since you're using auto layout, you should make sure that the image view's Compression Resistance and Content Hugging priorities are both greater than 500 (which is NSLayoutPriorityWindowSizeStayPut). That way, changes to the image view's intrinsic content size will override the system's tendency to keep the window the same size.

It would probably be good to set the image view to do no scaling, too, but I'm not sure it's necessary.

Also, you need to make sure that your other constraints don't have the effect of fixing the size of the image view. Obviously, you should not have width or height constraints on the image view itself, but you also need everything else to be flexible enough to allow the image view to be any size. If you need help with that, you're going to have to show us the constraints within the window.

Upvotes: 2

Related Questions