Solution for Ch31 Challenge: NerdSplitViewController

Create new file NerdSplitViewController.swift

import Cocoa

class NerdSplitViewController: NSViewController {
  let minimumViewWidth: CGFloat = 100
  let minimumViewHeight: CGFloat = 100
  
  func reset() {
    guard childViewControllers.count == 2 else {
      return
    }
    
    let firstView = childViewControllers[0].view
    firstView.translatesAutoresizingMaskIntoConstraints = false
    
    let separator = NSBox()
    separator.boxType = .separator
    separator.translatesAutoresizingMaskIntoConstraints = false
    
    let secondView = childViewControllers[1].view
    secondView.translatesAutoresizingMaskIntoConstraints = false

    view.subviews = [firstView, separator, secondView]
    let viewsDict = ["V0" : firstView, "separator" : separator, "V1" : secondView]
    let metricsDict = ["minWidth" : minimumViewWidth as NSNumber, "minHeight" : minimumViewHeight as NSNumber]
    
    func addVisualFormatConstraints(_ visualFormat:String) {
      let constraints = NSLayoutConstraint.constraints(withVisualFormat: visualFormat, options: [], metrics: metricsDict, views: viewsDict)
      NSLayoutConstraint.activate(constraints)
    }
    addVisualFormatConstraints("H:|[V0(>=minWidth)][separator(==1)][V1(==V0)]|")
    addVisualFormatConstraints("V:|[V0]|")
    addVisualFormatConstraints("V:|[separator(>=minHeight)]|")
    addVisualFormatConstraints("V:|[V1]|")
  }
  
  override func loadView() {
    view = NSView()
    reset()
  }
  
  override func insertChildViewController(_ childViewController: NSViewController, at index: Int) {
    super.insertChildViewController(childViewController, at: index)
    if isViewLoaded {
      reset()
    }
  }
  
  override func removeChildViewController(at index: Int) {
    super.removeChildViewController(at: index)
    if isViewLoaded {
      reset()
    }
  }
}

Update class AppDelegate

//    let tabViewController = NerdTabViewController()
//    tabViewController.addChildViewController(flowViewController)
//    tabViewController.addChildViewController(columnViewController)
    
    let splitViewController = NerdSplitViewController()
    splitViewController.addChildViewController(flowViewController)
    splitViewController.addChildViewController(columnViewController)
    
//    let window = NSWindow(contentViewController: tabViewController)
    let window = NSWindow(contentViewController: splitViewController)