13

How can I set the start position of a NSSplitView?

The closest thing I've found that looks like it would work is setPosition

//Set splitView position.
[splitView setPosition:330 ofDividerAtIndex:0];

This doesn't seem to do anything though, my splitview still starts with the divider in the center.

ANy ideas?

Bjorn
  • 69,215
  • 39
  • 136
  • 164
aroooo
  • 4,726
  • 8
  • 47
  • 81

4 Answers4

5

You don't set the position of the divider, you set the sizes of your NSSplitView's subviews. The divider is then repositioned automatically.

This is how I positioned my divider and subview size (in swift):

let subview: NSView = mySplitView.subviews[1] as NSView
subview.setFrameSize(NSMakeSize(subview.frame.size.width, 100))
Bjorn
  • 69,215
  • 39
  • 136
  • 164
4

In the view's class housing the split view

override func viewWillAppear() {
    self.mySplitView.setPosition(120, ofDividerAtIndex: 0)
}

or wherever you want it to start.

slashlos
  • 913
  • 9
  • 17
3

NSSplitView needs initial non-sized bounds to make them layout correctly. If your view has zero-size, then it will not show expected layout.

The best way is providing non-zero layout (this is what IB does), but sometimes this is impossible.

If you cannot provide non-zero size, then I think you have to provide proper - (void)splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize delegate method implementation to layout everything manually yourself. (this is my current best practice)

eonil
  • 83,476
  • 81
  • 317
  • 516
1

Maybe that is the center? If splitView is correctly hooked up to your split view, that code should work. You should probably log [splitView minPossiblePositionOfDividerAtIndex:0] and [splitView maxPossiblePositionOfDividerAtIndex:0] before trying to set the position of the divider so you know the possible values.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Yeah, it's not the center. My min value is 0 and my max value is 999. – aroooo Aug 17 '12 at 02:54
  • I don't know what to say -- it worked fine for me. I wonder if it has anything to do with constraints settings? I just dragged out a split view, added a few buttons and text fields to each side (so there would be something to see) and added that one line of code. – rdelmar Aug 17 '12 at 03:00
  • It seems the issue lies elsewhere, I have a setting that allows a user to choose their default view- if I set the view with the splitView as the default one then the split is in the center. If I set another view as the main one and click into the view with the splitView everything works fine. Any idea what might cause this? – aroooo Aug 17 '12 at 03:37
  • That seems odd, but I can't really tell without seeing the code. – rdelmar Aug 17 '12 at 03:45
  • You have to set the position of the divider after the view loads. A great place is the `- (void)viewWillAppear` method of its associated controller. I believe this relates to the fact that the `setPosition:ofDividerAtIndex:` method interacts with the delegate in order to take into account imposed constraints and the split view's relationship with it's subviews. Remember that a divider is a subview of the split view itself. – Thomas Anthony Jan 13 '16 at 22:59