How to set the minimum and initial width of the NSSplitView panel using auto-layout?

I am new to OS X cocoa and working on the Swift App using storyboards for Yosemite.

In the main window of my application, I have an NSSplitViewController. For each panel, left and right, I added a root user view that will contain all the subviews.

Storyboard layout

Using automatic layout, I attached the user view to the edges of the view in split mode and also defined a limit to the width of the user view that would be greater than or equal to the minimum number.

Autolayout constraints

All of this works great. When a window opens, the width of the window is the sum of the minimum width limits of the two panels. What I cannot understand is the next step.

How can I give Custom View another Width restriction so that when the window opens, the initial width is above the minimum (say 300)? I want the panel to be at level 200, but I want the initial size to be 300. I do not want the window to open with the minimum allowable width. Then the user will have to resize it every time, and this will not be a good user interface.

To try to accomplish this, I added another width limit for 300 with a lower priority (tried all different values ​​200, 500, 800, etc.) in the user view, but none of them gave me the desired width result, the value is 300, and the panel has a minimum width of 200.

Revised Autolayout constraints

There should be a way to do this, but I'm just fixated on how to do this. Please, help!

Update

Here is the result of the restrictions: The LayoutForOrientation effect for the NSSplitViewController view:

<NSLayoutConstraint:0x6000000923e0 H:|-(0)-[NSView:0x600000124c40] (Names: '|':NSView:0x600000124ba0 )>, <NSLayoutConstraint:0x6000000924d0 H:[NSView:0x600000124c40]-(0)-| (Names: '|':NSView:0x600000124ba0 )>, <NSLayoutConstraint:0x600000092070 H:[NSView:0x600000124c40(>=200)]>, <NSLayoutConstraint:0x600000092660 _NSSplitViewItemViewWrapper:0x6000001416b0.width == NSView:0x600000124ba0.width>, <NSLayoutConstraint:0x600000093a10 H:|-(0)-[_NSSplitViewItemViewWrapper:0x6000001416b0](LTR) (Names: '|':NSSplitView:0x608000123b60 )>, <NSLayoutConstraint:0x600000093b50 H:[_NSSplitViewItemViewWrapper:0x6000001416b0]-(1)-[_NSSplitViewItemViewWrapper:0x6000001429f0](LTR)>, <NSAutoresizingMaskLayoutConstraint:0x600000093d80 h=-&- v=-&- H:[NSSplitView:0x608000123b60]-(0)-| (Names: '|':NSView:0x6000001242e0 )>, <NSAutoresizingMaskLayoutConstraint:0x600000093b00 h=-&- v=-&- H:|-(0)-[NSSplitView:0x608000123b60] (Names: '|':NSView:0x6000001242e0 )>, <NSAutoresizingMaskLayoutConstraint:0x600000094050 h=-&- v=-&- H:|-(0)-[NSView:0x6000001242e0] (Names: '|':NSThemeFrame:0x100940be0'Main Window' )>, <NSLayoutConstraint:0x600000093c40 H:[_NSSplitViewItemViewWrapper:0x6000001429f0]-(0)-|(LTR) (Names: '|':NSSplitView:0x608000123b60 )>, <NSLayoutConstraint:0x600000092890 H:[NSView:0x600000124f60]-(0)-| (Names: '|':NSView:0x600000124ec0 )>, <NSLayoutConstraint:0x6000000926b0 H:[NSView:0x600000124f60(>=400)]>, <NSLayoutConstraint:0x608000099d20 _NSSplitViewItemViewWrapper:0x6000001429f0.width == NSView:0x600000124ec0.width>, <NSLayoutConstraint:0x600000092840 H:|-(0)-[NSView:0x600000124f60] (Names: '|':NSView:0x600000124ec0 )>] 

Due to the large number of restrictions for sorting, I decided to create a completely new test project and see if this is due to an error or some of the settings that I changed in my application. In a test project, I simply deleted the view controller, added the Split View controller to the window, and simply added two simple text views to each panel. Then, starting the project, the left hand panel was collapsed. So I added a minimum width limit for each text view, and then the same thing happened in the test project. Regardless of what I set in the storyboard, the size of the window is adding a minimum width. Perhaps the only way to overcome this is to set the window / split size in the code.

+5
source share
1 answer

You do not need to add additional restrictions to force this. The default window will be the size that it configured in the storyboard. If it becomes smaller, this indicates the presence of restrictions that force it less.

The window will maintain its size with a priority of 500 ( NSLayoutPriorityWindowSizeStayPut ). Restrictions of a higher priority can push or pull it to different sizes. Lower priority restrictions will be overridden by the current window size.

So, you should look for restrictions with a priority above 500 that try to pull it already. For example, content that spans the priorities of controls. Another possibility is the priority of retaining elements of a divided view. If they are above 500, and the initial size of the views loaded from the storyboard is minimal, then I expect that the split view will prefer to keep them in these sizes more than the window prefers to keep its size.

Perhaps a useful diagnostic method is log [someView constraintsAffectingLayoutForOrientation:NSLayoutConstraintOrientationHorizontal] . You can do this for a contentView or split view.

+6
source

All Articles