All what I am trying is, to add an NSImageView and make it fill the Window whenever I resize it.
However, as soon as the constraints are added, the window loses it's ability to resize.
If I add
view.translatesAutoresizingMaskIntoConstraints = false
the constraints are ignored too.
Could someone please point me where I am doing something wrong?
Code can be downloaded from here
Firstly, your parentView
is the window's contentView
. I can't think of a time when I've ever interfered with the default layout behaviour of this view so I'd remove the line parentView.translatesAutoresizingMaskIntoContraints = false
.
Secondly, by the looks of things you do want to take full responsibility for constraining the image view, so you for this view you should set translatesAutoresizingMaskIntoContraints
to false.
Finally, in addition to the constraints you add, all NSView
objects come with constraints that tell Auto Layout to what extent the view's content should effect how and whether it resizes. What's meant by content varies depending on the view, but in your case it's the NSImage
object. In your code you're getting the default behaviour, which basically says don't allow any content compression - you need to change this:
imageView.setContentCompressionResistancePriority(249, forOrientation: NSLayoutConstraintOrientation.Horizontal)
imageView.setContentCompressionResistancePriority(249, forOrientation: NSLayoutConstraintOrientation.Vertical)
Really, unless you've got a compelling reason not to, you should be setting your NSImageView
up in Interface Builder. Interface Builder not only does away with lines and lines code, it also provides live debugging help. For example, I answered this question by commenting out all of your code and redoing the whole thing up in Interface Builder first. The fact that the issue was a compression constraints thing was immediately obvious from the presence of the dashed orange warning lines that appeared whenever I selected the image view: