我发现你可以在NSSplitView
里面把旧式的换成新式的awakeFromNib
。示例代码如下。
我假设window
and oldSplitView
areIBOutlet
对应于 XIB 中的对象,以及两个虚拟NSViewController
sleftVC
和rightVC
在 XIB 中创建,它们的view
s 连接到左窗格和右窗格。那么你所要做的就是:
// 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;