5

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

Pop Flamingo
  • 3,023
  • 2
  • 26
  • 64
  • Did you resolve this issue? I face the same situation right now... – Blaszard Jan 23 '15 at 03:48
  • @Gardecolo Unfortunately no :( – Pop Flamingo Jan 23 '15 at 05:17
  • 1
    @Gardecolo But I just started a bounty ! :) – Pop Flamingo Jan 23 '15 at 05:21
  • I found that you can resolve this issue (in part) by avoiding `NSSplitViewController`. Just use `NSViewController` instead and put `NSSplitView` on top of that. This lets you set the initial position of the divider in IB. – Blaszard Jan 24 '15 at 01:35
  • For your information, `NSSplitViewController` seems to take a different approach; it takes `NSSplitView` as well as two or more `NSSplitViewItem` under it, and manages them. I still don't get how it manages them. `NSSplitViewController` is introduced first in OS X 10.10 and the documentation is so scarce... – Blaszard Jan 24 '15 at 01:41
  • By the way, if you use `NSSplitViewController` you must set Auto Layout on child views(https://developer.apple.com/library/mac/documentation/AppKit/Reference/NSSplitViewController_Class/). I think this may be the cause of your hidden subviews... – Blaszard Jan 24 '15 at 01:46

3 Answers3

7

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

fz.
  • 3,233
  • 22
  • 20
  • Works like a charm. If you want to embed all of your views into a custom view just simply select all your views and select Editor -> Embed In -> Custom View from the menu. Set all of your desired AutoLayout constraints: pin custom view to edges of superview, Set the minimum width (or height) of custom view. voila! Great answer. – wcochran Jul 07 '17 at 23:51
1

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

Jeremy Pope
  • 3,342
  • 1
  • 16
  • 17
0

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;
}
bluedome
  • 2,449
  • 1
  • 22
  • 17