I want to replace RBSplitView
with NSSplitView
in my existing project. The application is now leopard only and I would like to replace RBSplitView
with the new NSSplitView
shipped with Leopard.
However, I'm missing RBSplitView
's handy methods expand
and collapse
in NSSplitView
. How can I expand and collapse parts of NSSplitView programmatically?
I just got programmatic expanding and collapsing of
NSSplitView
to work. I've also configured myNSSplitView
to expand/collapse a subview whenever the divider is double-clicked, so I wanted this to play nice with that feature (and it seems to). This is what I did:(in this example,
splitView
is theNSSplitView
itself,splitViewSubViewLeft
is the subview I wish to expand/collapse andlastSplitViewSubViewLeftWidth
is an instance variable of typeCGFloat
.)I recommend to use
NSSplitViewController
instead, andNSSplitViewItem.isCollapsed
to control them. This just work.To make this to work properly, you have to configure split-UI components with mainly view-controllers. Otherwise, it can be broken.
Simply hide the subview you want to collapse, e.g.
You might also want to implement the delegate method -(BOOL)splitView:shouldHideDividerAtIndex: to return YES to hide the divider when a collapsed.
After some experimenting with the suggestions this was the easiest solution I found:
The function is a user defined first-responder action. It is triggered by a menu-item (or keystroke). The collapsibleView is a subview in the splitView both of which are connected in IB with their properties.
NSSplitView
actually has a private method-(void)_setSubview:(NSView *)view isCollapsed:(BOOL)collapsed
that does this. Those who would like to ignore all warnings against using private methods, behold:I implemented this as a category on
NSSplitView
. The only issue is that Xcode gives a warning about_setSubview:isCollapsed:
being undeclared... I'm not really sure how to get around that.El Capitan Update
I haven't written any code for OS X in ~2 years now so I haven't been able to verify this, but according to lemonmojo in the comments below,
_setSubview:isCollapsed:
was renamed in El Capitan to_setArrangedView:isCollapsed:
.You could try Brandon Walkin's BWToolKit.
The BWSplitView class has a method