5

I have an NSSplitView that uses autolayout to position the two subviews inside of it.

Everything works great, but I want to set the initial position of the divider to a constant value (300 pixels) for aesthetic reasons. I'm not using interface builder.

If I do [_splitView setPosition:300 ofDividerAtIndex:0];, I see no effect, same thing if I add a [_splitView adjustSubviews] call right after that.

Any tips?

user358829
  • 741
  • 1
  • 7
  • 17
  • Can you post more information about what constraints you have applied? NSScrollView with autolayout constraints are a bit of a grey area (at least to me), I haven't manage to find any guidelines. What happens if you remove your constraints, does the divider move? – Daniel Farrell Jan 25 '14 at 01:10
  • Isn't setting the position of the divider equivalent to setting the width of the view on the left side? Try setting the initial size of the left view to 300. – NSAdam Jan 26 '14 at 06:13
  • possible duplicate of [Starting Position of NSSplitView divider](http://stackoverflow.com/questions/11995073/starting-position-of-nssplitview-divider) – eonil May 26 '14 at 02:18

2 Answers2

5

I met the same issue. It seems that setPosition has no effect if invoked right after you add subviews to the splitView.

I solve the problem by waiting a little while before invoking setPosition. Here is the sample code in swift:

func delay(delay:Double, closure:()->()) {
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
    dispatch_after(time, dispatch_get_main_queue(), closure)
}

delay(0.05) {
    splitView.setPosition(300, ofDividerAtIndex: 0)
}
Tyler Liu
  • 19,552
  • 11
  • 100
  • 84
4

I met the same issue, but I solved it calling setPosition from viewDidAppear. Hope this helps.

- (void)viewDidAppear {
  [self.splitView setPosition:300 ofDividerAtIndex:0];
}
Antonio J.
  • 41
  • 1