Search code examples
objective-cmacoscocoainterface-buildernstoolbar

NSToolbarItem Selection Does Not Cover Text


I have an app with a standard NSWindow that contains a NSToolbar with a few NSToolbarItems to act as a preference items. The NSToolbarItems are set as selectable, and use the included "Template" as part of their name to get the nice system gradient effect.

However, when the NSToolbarItem is selected, the selection does not change the background of the text.

Each NSToolbarItem is marked as selectable, and my NSToolbarDelegate implements these two methods only:

-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar{
    NSMutableSet *toolbarSet = [NSMutableSet set];
    for (NSDictionary *prefItem in _preferenceItems){
        [toolbarSet addObject:[prefItem objectForKey:@"toolbarIdentifier"]];
    }
    return [toolbarSet allObjects];
}

-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar{
    NSMutableSet *selectableSet = [NSMutableSet set];
    for (NSDictionary *prefItem in _preferenceItems){
        [selectableSet addObject:[prefItem objectForKey:@"toolbarIdentifier"]];
    }
    return [selectableSet allObjects];
}

Here's what I'm seeing; notice the gray box around the text:

behavior

As opposed to Safari's Preferences window:

safari

Configuration of all of my NSToolbarItems in Interface Builder:

ib

And the NSToolbar itself:

toolbar


Solution

  • As a part of the setup of my NSToolbarDelegate / NSWindowController I set:

    [[[self window] contentView] setWantsLayer:YES];
    

    Commenting this line out resolved the issue.

    In Context:

    -(void)awakeFromNib{
         NSDictionary *zendeskItem = @{@"name" : @"Zendesk Accounts",
                                      @"toolbarIdentifier" : @"zendesk"};
         NSDictionary *toolItem = @{@"name" : @"Tools",
                                      @"toolbarIdentifier" : @"tools"};
         NSDictionary *supportItem = @{@"name" : @"Support",
                                      @"toolbarIdentifier" : @"support"};
         NSDictionary *healthItem = @{@"name" : @"Health",
                                      @"toolbarIdentifier" : @"health"};
         NSDictionary *aboutItem = @{@"name" : @"About",
                                     @"toolbarIdentifier" : @"about"};
    
         _preferenceItems = [NSArray arrayWithObjects:zendeskItem, toolItem, supportItem, healthItem, aboutItem, nil];
    
        [[self window] setContentSize:[_zendeskView frame].size];
        [[[self window] contentView] addSubview:_zendeskView];
        //[[[self window] contentView] setWantsLayer:YES];
    
        currentViewTag = 1;
    }