1

According to this WWDC 2020 talk, Big Sur's toolbar is automatically sectioned to match and follow the panes of the NSSplitView below.

Unfortunately, this requires us to use the post-NSViewController version of NSSplitView api, introduced in 10.10 Yosemite (see the corresponding WWDC 2014 session PDF here.)

What would be the easiest way to use it from a really old codebase with pre-NSViewController NSSplitView in XIB?

The new-style NSSplitView is famously unavailable in XIB and only available in the storyboard, as discussed in this StackOverflow post.

Yuji
  • 34,103
  • 3
  • 70
  • 88

1 Answers1

1

I found that you can swap the NSSplitView from the old-style one to the new-style one inside awakeFromNib. A sample code follows.

I assumed window and oldSplitView are IBOutlet to corresponding objects within XIB, and two dummy NSViewControllers leftVC and rightVC are created within XIB whose views are connected to the left pane and right pane. Then all you have to do is:

    // enable scrolling behind the tool bar, if you haven't
    window.styleMask|=NSWindowStyleMaskFullSizeContentView;

    // create a new-style NSSplitView using NSSplitViewController
    splitVC=[[NSSplitViewController alloc] init];
    splitVC.splitView.vertical=YES;
    splitVC.view.translatesAutoresizingMaskIntoConstraints=NO;

    // prepare the left pane as a sidebar
    NSSplitViewItem*a=[NSSplitViewItem sidebarWithViewController:leftVC];
    [splitVC addSplitViewItem:a];
    a.canCollapse=NO;

    // prepare the right pane
    NSSplitViewItem*b=[NSSplitViewItem splitViewItemWithViewController:rightVC];
    [splitVC addSplitViewItem:b];

    // swap the old NSSplitView with the new one
    [window.contentView replaceSubview:oldSplitView with:splitVC.view ];

    // set up the constraints so that the new `NSSplitView` to fill the window
    [splitVC.view.topAnchor constraintEqualToAnchor:window.contentView.topAnchor
                                           constant:0].active=YES;
    [splitVC.view.bottomAnchor constraintEqualToAnchor:((NSLayoutGuide*)window.contentLayoutGuide).bottomAnchor].active=YES;
    [splitVC.view.leftAnchor constraintEqualToAnchor:((NSLayoutGuide*)window.contentLayoutGuide).leftAnchor].active=YES;
    [splitVC.view.rightAnchor constraintEqualToAnchor:((NSLayoutGuide*)window.contentLayoutGuide).rightAnchor].active=YES;
Yuji
  • 34,103
  • 3
  • 70
  • 88