Search code examples
macoscocoansbuttonnstoolbar

Add navigation buttons to unified titlebar/toolbar


Is there a way to get the the default NSButtons used throughout OS X for navigating through a window's contents? The buttons I'm speaking of are shown in Finder, System Preferences, Safari, Mac App Store, Game Center, etc.

enter image description here

While I could recreate them using my own NSButton and custom images for the chevrons, I would like to use these buttons if they're provided to ensure they're the exact same style, and they will update when this style undoubtedly changes appearance in the future.


Solution

  • NSSegmentedControl + NSImageNameGoLeftTemplate + NSImageNameGoRightTemplate (some (most?) of this can be specified via the nib)

    [self.segmentedControl setSegmentCount:2];
    self.segmentedControl.segmentStyle = NSSegmentStyleSeparated;
    [[self.segmentedControl cell] setTrackingMode:NSSegmentSwitchTrackingMomentary];
    
    [self.segmentedControl setImage:[NSImage imageNamed:NSImageNameGoLeftTemplate] forSegment:0];
    [self.segmentedControl setImage:[NSImage imageNamed:NSImageNameGoRightTemplate] forSegment:1];
    
    [[self.segmentedControl cell] setTag:0 forSegment:0];
    [[self.segmentedControl cell] setTag:1 forSegment:1];
    
    [self.segmentedControl setWidth:23.0 forSegment:0];
    [self.segmentedControl setWidth:23.0 forSegment:1];
    
    [self.segmentedControl setEnabled:YES forSegment:0];
    [self.segmentedControl setEnabled:YES forSegment:1];
    
    [self.segmentedControl sizeToFit];
    

    The NSImages are documented here: System-Provided Images for Use in Controls