Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSSplitviewController programmatically

I have a NSSplitViewController with 2 view controllers as splitViewItems. This works fine.

Now I would like to set an own SplitViewController Class for my Splitviewcontroller in the storyboard. I give it my own class start the app. But now I only see an empty window.

Have I set my splitviewcontroller now programmatically, if I set my own splitviewcontroller class?

If yes, which code I have to use to show the two view controllers in my splitview controller again?

UPDATE

import Cocoa

class SplitViewController: NSSplitViewController {
    override func viewDidLoad() {
       print("Test")
    }
}

enter image description here

like image 259
Ghost108 Avatar asked Jun 01 '17 13:06

Ghost108


Video Answer


1 Answers

Here is an Xcode 9 Playground (Swift 4) which shows how to setup NSSplitViewController from code.

import Cocoa
import PlaygroundSupport

class ViewController: NSViewController {

   private let backgroundColor: NSColor

   init(backgroundColor: NSColor) {
      self.backgroundColor = backgroundColor
      super.init(nibName: nil, bundle: nil)
   }

   required init?(coder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
   }

   override func loadView() {
      view = NSView()
      view.wantsLayer = true
      view.layer?.backgroundColor = backgroundColor.cgColor
   }
}


class MainSplitViewController: NSSplitViewController {

   private let splitViewResorationIdentifier = "com.company.restorationId:mainSplitViewController"

   lazy var vcA = ViewController(backgroundColor: .red)
   lazy var vcB = ViewController(backgroundColor: .green)
   lazy var vcC = ViewController(backgroundColor: .blue)

   override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
      super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
      setupUI()
      setupLayout()
   }

   required init?(coder: NSCoder) {
      super.init(coder: coder)
   }

}


extension MainSplitViewController {

   private func setupUI() {

      view.wantsLayer = true

      splitView.dividerStyle = .paneSplitter
      splitView.autosaveName = NSSplitView.AutosaveName(rawValue: splitViewResorationIdentifier)
      splitView.identifier = NSUserInterfaceItemIdentifier(rawValue: splitViewResorationIdentifier)
   }

   private func setupLayout() {

      minimumThicknessForInlineSidebars = 180

      let itemA = NSSplitViewItem(sidebarWithViewController: vcA)
      itemA.minimumThickness = 80
      addSplitViewItem(itemA)

      let itemB = NSSplitViewItem(contentListWithViewController: vcB)
      itemB.minimumThickness = 100
      addSplitViewItem(itemB)

      let itemC = NSSplitViewItem(viewController: vcC)
      itemC.minimumThickness = 80
      addSplitViewItem(itemC)
   }

}

let vc = MainSplitViewController()
vc.view.frame = CGRect(x: 0, y: 0, width: 400, height: 300)

PlaygroundPage.current.liveView = vc

UPDATE:

Version similar to one above but targeted to macOS 10.10.

import Cocoa
import PlaygroundSupport

class ViewController: NSViewController {

   private let backgroundColor: NSColor

   init(backgroundColor: NSColor) {
      self.backgroundColor = backgroundColor
      super.init(nibName: nil, bundle: nil)
   }

   required init?(coder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
   }

   override func loadView() {
      view = NSView()
      view.wantsLayer = true
      view.layer?.backgroundColor = backgroundColor.cgColor
   }
}


class MainSplitViewController: NSSplitViewController {

   private let splitViewResorationIdentifier = "com.company.restorationId:mainSplitViewController"

   lazy var vcA = ViewController(backgroundColor: .red)
   lazy var vcB = ViewController(backgroundColor: .green)
   lazy var vcC = ViewController(backgroundColor: .blue)

   override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
      super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
      setupUI()
      setupLayout()
   }

   required init?(coder: NSCoder) {
      super.init(coder: coder)
   }

}


extension MainSplitViewController {

   private func setupUI() {

      view.wantsLayer = true

      splitView.dividerStyle = .paneSplitter
      splitView.autosaveName = NSSplitView.AutosaveName(rawValue: splitViewResorationIdentifier)
      splitView.identifier = NSUserInterfaceItemIdentifier(rawValue: splitViewResorationIdentifier)

      vcA.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true
      vcB.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true
      vcC.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true
   }

   private func setupLayout() {

      let sidebarItem = NSSplitViewItem(viewController: vcA)
      sidebarItem.canCollapse = true
      sidebarItem.holdingPriority = NSLayoutConstraint.Priority(NSLayoutConstraint.Priority.defaultLow.rawValue + 1)
      addSplitViewItem(sidebarItem)

      let xibItem = NSSplitViewItem(viewController: vcB)
      addSplitViewItem(xibItem)

      let codeItem = NSSplitViewItem(viewController: vcC)
      addSplitViewItem(codeItem)
   }

}

let vc = MainSplitViewController()
vc.view.frame = CGRect(x: 0, y: 0, width: 400, height: 300)

PlaygroundPage.current.liveView = vc
like image 55
Vlad Avatar answered Oct 18 '22 22:10

Vlad