Why is my split view behaving like this and how can I fix it?

2k Views Asked by At

I created a split view controller that displays two views, like this : enter image description here

When I compile it, it gives me this result : enter image description here

Unfortunately, the first view isn't visible and I must drag from the left hand side of the window to see the two views :
enter image description here

First, why is the split view behaving like this ? Why isn't it already at the right size from the beginning ?
If I add this line to the viewDidLoad() function of my SplitViewController :

splitView.adjustSubviews()

Then the two view appears, with equal size, but I don't understand what the adjustSubviews() function does exactly, and I can't control the position of either.

How to fix it programmatically ? How to adjust the size of each view ? Is there a way to adjust it in interface builder ?

Thank you.

EDIT : There is now a bounty of 50 points for this question

3

There are 3 best solutions below

1
On BEST ANSWER

Since NSSplitViewController is auto-layout based, you can use auto-layout either if you want a fixed or a dynamic behavior on your views.

For a fixed width what you can do is add a custom view inside your 2 views with a 0 constraint to all the edges and a fixed width. For example:

enter image description here

Then the window will get expanded as follows:

enter image description here

0
On

We need to set sizes of NSSplitView's sub-views directly. Setting ones through splitViewItems seems to have no effect.

// NSSplitViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    NSView *view1 = self.splitView.subviews[0];
    NSRect rect = view1.frame;
    rect.size.width = 150;
    view1.frame = rect;

    NSView *view2 = self.splitView.subviews[1];
    rect = view2.frame;
    rect.size.width = 300;
    view2.frame = rect;
}
0
On

You should be able to set the position of the divider in the split view using (assuming 2 views in the split view):

self.splitView.setPosition(200.0, ofDividerAtIndex: 0)

That would set the first divider at position 200.0