Getting Bottom Bar Buttons to Follow an NSSplitView Pane (Code, wonky)

2010-10-26 20:00:00 -0400

In working on Strip for Mac OS X, I came across a scenario in which I wanted to get the bottom bar buttons on an NSWindow to follow the left-margin of panes about them in an NSSplitView with which they are associated. That sounds like a mouth full, so I’ll use a picture to illustrate, it’s an idea I’m looking to borrow from Apple’s Address Book application (click image to embiggen):

Adressbook Splitview 500w

What I’m trying to suggest with the annotations I’ve made on the image is that the [+] button under that middle pane (highlighted in green) will move as the left margin of the pane is moved, either by the user, or by the window being resized—which cascades down to the subview being resized, since the split view is basically the entirety of the window’s contents.

In any event, I wasn’t sure how to hack this out and didn’t see any examples out there, so I put one together. There might be a better, faster way of handling this, perhaps by responding to NSView notifications about a change in frame, but here I just used an NSSplitViewDelegate method that fires every time subviews are resized (thus, every time the margins of the panes might have been adjusted by a resize or a divider moving). Sample project is here on Github, but this is the meat of it, in the NSSplitView’s delegate:

// gets call after the NSSplitView is resized OR after the divider(s) are moved
- (void)splitViewDidResizeSubviews:(NSNotification *)aNotification
[self repositionBottomBarButtons];

- (void)repositionBottomBarButtons
// we don't actually have to do anything for the left-most button and the left pane
// because we've got it pegged in IB to maintain it's position in relation to the
// window's bottom and left bounds

// first tell the superview that it will need to redraw the frame we'll be moving
NSRect oldFrame = [rightButton frame];
[[rightButton superview] setNeedsDisplayInRect:oldFrame];

NSPoint newOrigin = rightView.frame.origin;

NSRect newFrame = NSMakeRect(newOrigin.x, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);
[rightButton setFrame:newFrame];
[rightButton setNeedsDisplay:YES];

And there we go, button now follows the pane’s margin, moves nice and smooth.

blog comments powered by Disqus