Setting minimum width of NSSplitViews

Mike Pulsifer picture Mike Pulsifer · Jan 17, 2015 · Viewed 8k times · Source

I'm having a heck of a time setting up a simple split view. The first split view is collapsed. I need to set a minimum width for it. Everything I see online (scarce for NSSplitViewController/NSSplitView) is for Objective-C, puts everything in the app delegate, and uses XIBs.

Here's the scenario: Window Controller with a segue to a SplitView Controller, which has two split views (2 view controllers).

Which object needs to have the NSSplitViewDelegate?

EDIT: Adding code snippet: For example, I have this:

import Cocoa

class ViewController: NSSplitViewController, NSSplitViewDelegate {


    @IBOutlet weak var pdlSplitView: NSSplitView!

    override func viewDidLoad() {
        super.viewDidLoad()

        }
    override func splitView(splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
        return proposedMinimumPosition + 200
    }

}

Is there more that I'm missing?

Thanks

UPDATE

Based on comments below, I've made a change, but now I get a sigAbort on the class definition for the AppDelegate. Full code

ViewController:

import Cocoa

class ViewController: NSSplitViewController, NSSplitViewDelegate {


    @IBOutlet weak var pdlSplitView: NSSplitView!

    let publicDataListings : PDL = PDL()


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.pdlSplitView.delegate = self

    }

    override func splitView(splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
    return proposedMinimumPosition + 200
    }

}

SidebarViewController:

import Cocoa

class SidebarViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }


}

DatasetViewController:

import Cocoa

class DatasetViewController: NSViewController, NSSplitViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }

}

Update I took away my custom NSSplitViewController class and created two NSSplitView classes, one with the constraint method. Now, I see both subviews, but they're far smaller than they should be:

enter image description here

Is there anyone at all that has done this with Swift and Storyboards?

Answer

Mythlandia picture Mythlandia · Jan 3, 2016

No coding is required to set a minimum width in a storyboard with auto layout for a NSSplitViewController/NSSplitView.

Select the CustomView that you require a minimum width for (e.g. 200), and add a width constraint set to the required value which will add a "Equal" constraint (e.g. Custom View.Width equals 200).

Next locate that new constraint and change the constraint relation to "Greater Than or Equal" (e.g. so you now have width ≥ 200).

You now have a minimum width in an NSSplitView. You can then use the Priority field to resolve any conflicts with any other auto layout constraints.