How do you explicitly set the width of an NSView that already has constraints dictating its minimum width and its maximum width?
For example, consider an NSSplitView
embedded in a window. In the left pane of the split view is an Inspector
while in the right pane is just some content:
---------------------------------
| | | <- NSSplitView
| | |
| Inspector | Content |
| | |
| | |
| | |
---------------------------------
---------------------------------
| |
| |
| Content |
| |
| |
| |
---------------------------------
The Inspector
has two constraints on it:
width >= 200.0 @ 1000
width <= 400.0 @ 1000
The only constraints on the split view are ones that allow it to fill its superview and the Content
view only as constraints to fill its superview as well (the right-side splitter pane).
When this view is created, I'd like to set the initial width of the Inspector
to a dynamic value (something that was saved in the user's preferences, perhaps).
But if I just naïvely do something like:
width = 320.0 @ 1000
Then the split view will no longer allow that view to be resized. Values of 499
and 749
also cause the same problem. If I lower the priority to something like:
width = 320.0 @ 250
Then the split view will resize, but its initial pane width is not 320.0
but rather 400.0
, as set laid-out in Interface Builder.
The ultimate goal is to allow the Inspector
to be both resized within a given range, but to also be hidden or collapsed entirely (of which I was hoping to just set the width
to 0
, and the minWidth
constraint to 0
as well).
I suspect I could go down and handle this manually in [layout]
but I'm curious if there's a way to accomplish this entirely with constraints.
(Note: Most of this layout is being done in Interface Builder but I'm quite happy to handle the constraints programmatically if need-be. 10.8+ is fine as well.)
Have you tried using -[NSSplitView setAutosaveName:] to have NSSplitView do the work to save the user's preference and then load that as the initial width on startup?
If you didn't want to go that route, you could do something pretty similar to your first instinct.
After you add the width = 320.0 @ 1000
constraint, you want to layout your window, [window layoutIfNeeded]
, and then remove that constraint.
Your Inspector view will have the updated frame, but the constraint forcing it to be there will be gone, allowing the view to be resized.