Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

We have a parent Split view (NSSplitView), and two subviews, Content and SideBar (the sidebar is on the right).

What would be the optimal Cocoa-friendly way to toggle the SideBar view?

  • I would really love it, if the suggested solution includes animation
  • I really don't need any suggestions related to external plugins, etc (e.g. BWToolkit)

HINT : I've been trying to do that, but still I had issues hiding the divider of the NSSplitView as well. How could I do it, while hiding it at the same time?

like image 702
Dr.Kameleon Avatar asked Mar 23 '12 10:03

Dr.Kameleon


3 Answers

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.

like image 122
Nathan Kinsinger Avatar answered Nov 02 '22 21:11

Nathan Kinsinger


Easiest way to do it is as follows - and it's animated: [SWIFT 5]

    splitViewItems[1].animator().isCollapsed = true // Show side pane
    splitViewItems[1].animator().isCollapsed = false // hide side pane
like image 6
rs7 Avatar answered Nov 02 '22 22:11

rs7


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.

like image 4
Bjorn Avatar answered Nov 02 '22 22:11

Bjorn