Set NSSplitViewController Pane's Width Using Swift

3.4k views Asked by At

Xcode 8.2.1, Swift 3.0.2, macOS 10.12

I have an NSSplitViewController that has 3 panes (please note the NS; this is for a Mac app).

I'm trying to set the left-most pane to always have a fixed width and not be resizable. I have tried the following:

  • For some reason, auto layout constraints are not available in the Storyboard editor on the child view of the pane.
  • Setting constrainMaxCoordinate and constrainMinCoordinate didn't work.

Has anyone else gotten this to work lately? There are lots of articles about this topic, but their toolsets are several years old and none of their suggestions have worked for me. Thanks!

enter image description here

3

There are 3 answers

0
Clifton Labrum On

I finally figured this out. I had to set an auto layout constraint programmatically. Setting a constraint on an NSSplitViewItem was not available in the storyboard editor (Xcode 8.1).

Here's how I did it:

//This is my SplitViewController subclass
class MainSplitVC: NSSplitViewController {

  //Outlet to the menu pane
  @IBOutlet weak var menuPane: NSSplitViewItem!

  override func viewDidLoad() {
    super.viewDidLoad()

    //Hard-code the width of the menu pane
    NSLayoutConstraint(item: menuPane.viewController.view, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 80).isActive = true
  }
}

I hope that helps someone else.

2
Daniel On

Usually you should set a layout constraints to subviews of the child view.

For example, if you have an NSOutlineView in the left pane, just set it's width constraint to a fixed value.

The size of a pane's view is calculated by the constraints of it's subviews.

enter image description here enter image description here

0
Ely On

In case you don't use layout constraints for setting the width of a NSSplitViewItem, you can use code like this:

splitViewController.splitView.setPosition(600, ofDividerAt: 0)