10

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.

Pierre Houston
  • 1,631
  • 20
  • 33
  • Can't you just set a width constraint on the custom view (placeholder)? – codingFriend1 Sep 03 '12 at 10:49
  • 1
    Edited my post to make this more clear, I hope. My placeholder already has constraints to give it the right behavior when resizing the window. Adding fixed size constraints to it when adding the subview would break that. – Pierre Houston Sep 06 '12 at 16:01
  • If your constraints are correct then the window resize would be working. – deleted_user Sep 17 '12 at 15:19
  • (replacing my misleading comment) But the window resize is working. When there's no subview added to my placeholder, its simple and works perfectly. After subview is added and window automatically changes size, resizing works fine then too. Next time I'm in this code, I'll ensure that translateAutoresizingMask isn't accidentally set to ON for the subview, this has cause me problems in the past. – Pierre Houston Oct 25 '12 at 21:10
  • @smallduck I find it hard to understand your problem from pure text. Could you add a screenshot or wireframe to illustrate the problem, please? – JJD Oct 29 '12 at 10:08

1 Answers1

14

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:]

iain
  • 5,660
  • 1
  • 30
  • 51
  • It sound right that this is a priority issue. However, in the case that prompted this posting, the window was larger than what the added subview needed. The subview wouldn't have been compressed, but instead expanded (the problem was that it didn't expand but instead the window shrunk). Does this mean the compression resistance priority wouldn't come into play? Must the Content Hugging priority instead also be set to <500, or am I confused? (I've been working with auto-layout for almost a year and I still find it difficult to visualize the purpose of those 2 sets of priorities). – Pierre Houston Mar 01 '13 at 20:03
  • yes, you're correct, the content hugging priority should be < 500 instead.Compression resistance priority is used when working out which view should shrink when reducing the space, and content hugging when increasing the space. With the window stay put priority of 500,then anything <500 means that the window size wins, >500 the view wins – iain Mar 02 '13 at 13:06
  • Now named: `NSLayoutConstraint.Priority.windowSizeStayPut` – Sentry.co May 31 '21 at 12:09