I first tried to copy the code from the second section of the chapter (“View Swapping”):
class NerdTabViewController: NSViewController {
//var box = NSBox()
var buttons: [NSButton] = []
var contentView: NSView?
func selectTabAtIndex(index: Int) {
assert((0..<childViewControllers.count).contains(index), "index out of range")
for (i, button) in buttons.enumerate() {
button.state = (index == i) ? NSOnState : NSOffState
}
let viewController = childViewControllers[index]
//box.contentView = viewController.view
contentView?.removeFromSuperview()
view.addSubview(viewController.view)
contentView = viewController.view
}
And copied the simple substitution into the “reset” method:
//box.translatesAutoresizingMaskIntoConstraints = false
//box.borderType = .NoBorder
//box.boxType = .Custom
contentView?.translatesAutoresizingMaskIntoConstraints = false
let separator = NSBox()
separator.boxType = .Separator
separator.translatesAutoresizingMaskIntoConstraints = false
view.subviews = [stackView, separator, /*box*/]
var views = ["stack": stackView, "separator": separator, /*"box": box*/]
let metrics = ["buttonHeight": buttonHeight]
func addVisualFormatConstraints(visualFormat: String) {
let constraints = NSLayoutConstraint.constraintsWithVisualFormat(visualFormat, options: [], metrics: metrics, views: views)
NSLayoutConstraint.activateConstraints(constraints)
}
addVisualFormatConstraints("H:|[stack]|")
addVisualFormatConstraints("H:|[separator]|")
if let contentView = contentView {
view.subviews.append(contentView)
views["content"] = contentView
addVisualFormatConstraints("H:|[content(>=100)]|")
addVisualFormatConstraints("V:|[stack(buttonHeight)][separator(==1)][content(>=100)]|")
} else {
addVisualFormatConstraints("V:|[stack(buttonHeight)][separator(==1)]|")
}
//addVisualFormatConstraints("H:|[box(>=100)]|")
//addVisualFormatConstraints("V:|[stack(buttonHeight)][separator(==1)][box(>=100)]|")
But when I ran the code, the main view deflated to zero size, leaving just the buttons. The main view is actually there; I see the bottom-left corner of the art drawing over the buttons!
I don’t grok what “need to resize the view with its container” (from the “View Swapping” section) means. I tried adding:
let views = ["content": contentView!]
let constraints = NSLayoutConstraint.constraintsWithVisualFormat("|[content]|", options: [], metrics: nil, views: views) + NSLayoutConstraint.constraintsWithVisualFormat("V:|[content]|", options: [], metrics: nil, views: views)
NSLayoutConstraint.activateConstraints(constraints)
at the end of “selectTabAtIndex,” but the window collapsed to nothing (just the title bar) with a bunch of console errors regarding conflicts. So the constraints set up in the “reset” method are there, but why don’t they help inflate the content view and/or its container to the proper size?