Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set NSSplitViewController Pane's Width Using Swift

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

like image 771
Clifton Labrum Avatar asked Dec 22 '16 20:12

Clifton Labrum


2 Answers

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.

like image 181
Clifton Labrum Avatar answered Nov 08 '22 23:11

Clifton Labrum


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

like image 1
Daniel Avatar answered Nov 09 '22 00:11

Daniel