How to toggle visibility of NSSplitView subView + hide Pane Splitter divider?

Here's a pretty decent tutorial that shows how to do this: Unraveling the Mysteries of NSSplitView.

Hiding the divider is done in NSSplitView's delegate method splitView:shouldHideDividerAtIndex:.

You will have to animate the frame size change yourself if you don't like the way NSSplitView does it.


I wrote a Swift version of the content in the link from @Nathan's answer that works for me. In the context of my example splitView is set elsewhere, probably as an instance property on an encompassing class:

func toggleSidebar () {
    if splitView.isSubviewCollapsed(splitView.subviews[1] as NSView) {
        openSidebar()
    } else {
        closeSidebar()
    }
}

func closeSidebar () {
    let mainView = splitView.subviews[0] as NSView
    let sidepanel = splitView.subviews[1] as NSView
    sidepanel.hidden = true
    let viewFrame = splitView.frame
    mainView.frame.size = NSMakeSize(viewFrame.size.width, viewFrame.size.height)
    splitView.display()
}

func openSidebar () {
    let sidepanel = splitView.subviews[1] as NSView
    sidepanel.hidden = false
    let viewFrame = splitView.frame
    sidepanel.frame.size = NSMakeSize(viewFrame.size.width, 200)
    splitView.display()
}

These functions will probably methods in a class, they are for me. If your splitView can be nil you obviously have to check for that. This also assumes you have two subviews and the one at index 1, here as sidePanel is the one you want to collapse.


In Xcode 9.0 with Storyboards open Application Scene select View->Menu->Show sidebar. CTRL-click Show Sidebar, in sent actions delete the provided one, click on x. From the circle CTRL drag to First Responder in application scene and select toggleSideBar to connect to. Open storyboard and select the first split view item and in attributes inspector change behaviour from default to sidebar. Run and try with view menu item show/hide. All done in interface builder no code. toggleSideBar handles the first split view item. https://github.com/Dis3buted/SplitViewController