Question

I have a nib with autolayout enabled containing a view controller's views and, separately, a window with a placeholder view to add them it into. The placeholder has constraints relative to its superview and other views in the window, and before involving my view controller, I have the placeholder resizing with the window the way I want. Later I load the nib and add its top-level view as subview of the placeholder, and I also manually create constraints to keep it aligned to the placeholder's edges.

All good, and I'm using this pattern in several instances within my app, but in some of those cases, after adding the view my window gets resized! It's shrinking to the minimal size for the added view instead of the view expanding to the current size of the window.

So far the only way I've found to prevent this is that when adding the view and creating its constraints, to also set the view's frame to give it the initial size for the current state of the placeholder and window. That's ok some of the time, but I want to be able to define constraints that are more complicated that simply aligning each edge to a placeholder superview. I don't want to code the frame arithmetic for each of those cases.

Does anyone know what's going on? If the window is already dictating the size of my placeholder, not the other way around, what do I have to do when adding the subview to make the current window size still take precedence?

I've seen the pop-up menu in Xcode's IB windows that seems to be about constraints affecting only subviews vs also superview (set to the former for all my nibs), is what's happening related to this functionality? I haven't found anything else about that yet.

Was it helpful?

Solution

You want to set the compression resistance priority to something less than NSLayoutPriorityWindowSizeStayPut which is 500. This means that the size of the window takes priority over the size of the view.

You can set this from the IB size inspector.

Compression resistance priority

Or you can set it programmatically with -[NSView setContentCompressionResistancePriority:forOrientation:]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top