5

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

Andriy
  • 2,767
  • 2
  • 21
  • 29
Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • Just set the name of the `UISplitViewController` in the Storyboard as your own class. And you can do the same for the master and detail VCs. – koen Jun 01 '17 at 13:39
  • 1
    yes i set my custom class to the splitviewcontroller. after that i always see a empty window. if i remove the custom class all will be fine. notice: i need a OS X solution. uisplitviewcontroller is ios – Trombone0904 Jun 01 '17 at 13:41
  • Ah, I missed that you are on macOS. It sounds like you have some missing connections in your storyboard. Does your code even reach `viewDidLoad` (or the equivalent on macOS) in your custom VCs? – koen Jun 01 '17 at 13:54
  • yes it does reach the viewdidload – Trombone0904 Jun 01 '17 at 16:38
  • Please share some relevant code and maybe a screenshot of your storyboard. – koen Jun 01 '17 at 18:18
  • please look to my first post :) – Trombone0904 Jun 01 '17 at 18:47
  • any one have code with solution of example type resources plz give me ..of NSSplitView – ronak patel Jul 28 '17 at 11:55

1 Answers1

14

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
Vlad
  • 6,402
  • 1
  • 60
  • 74