1

I'm working with Cocoa and I create my views in code (no IB) and I'm hitting an issue with NSSplitView.

I have a NSSplitView that I configure in the following way in my view controller, in Swift:

override func viewDidLoad() {
    super.viewDidLoad()
    let splitView = NSSplitView()
    splitView.isVertical = true
    splitView.addArrangedSubview(self.createLeftPanel())
    splitView.addArrangedSubview(self.createRightPanel())
    splitView.adjustSubviews()
    self.view.addSubview(splitView)
    ...
}

The resulting view shows the two subviews and the divider for the NSSplitView, and one view is wider than the other. When I drag the diver to change the width, as soon as I release the mouse, the divider goes back to its original position, as if pulled back by a "spring".

I can't resize the two subviews; the right one always keeps a fixed size. However, nowhere in the code I fix the width of that subview, or any of its content, to a constant.

What I would like to achieve instead is that the right view size is not fixed, and that if I drag the divider at halfway through, the subviews will resize accordingly and end up with the same width.

This is a screen recording of the problem:

enter image description here

Edit: here is how I set the constraints. I'm using Carthography, because otherwise setting constraints in code is extremely verbose beyond the most simple cases.

private func createLeftPanel() -> NSView {
    let view = NSView()
    let table = self.createTable()
    view.addSubview(table)

    constrain(view, table) { view, table in // Cartography magic.
        table.edges == view.edges // this just constraints table.trailing to
                                  // view.trailing, table.top to view.top, etc.
    }
    return view
}

private func createRightPanel() -> NSView {
    let view = NSView()
    let label = NSTextField(labelWithString: "Name of item")
    view.addSubview(label)

    constrain(view, label) { view, label in
        label.edges == view.edges
    }
    return view
}
Marco83
  • 1,181
  • 1
  • 10
  • 28
  • Set constraints. – El Tomato Feb 26 '18 at 08:31
  • @ElTomato I'm editing the answer to show how I set the constraints. – Marco83 Feb 26 '18 at 23:08
  • 1
    If you think it might be constraints, Xcode is your friend. Xcode has a magnificent UI visualization tool. Simply run your app in the debugger, open the window in question, and then click the Debug View Hierarchy button in the debugger pane. I suggest turning on the "view constraints" option and then start exploring what constraints actually got created. – James Bucanek Feb 27 '18 at 17:16

0 Answers0