Highlight NSToolbarItems

后端 未结 3 773
失恋的感觉
失恋的感觉 2021-01-05 08:17

I want to highlight the selected NSToolbarItem like e.g. in Adium (see screenshot).

highlight http://a2.s3.p.quickshareit.com/files/screenshot_b28b67ba9

3条回答
  •  广开言路
    2021-01-05 08:50

    To expand upon Chuck's answer, you simply need to make your controller the delegate of your NSToolBar and implement the toolbarSelectableItemIdentifiers: delegate method in it. For example, the following implementation will let you retain the selection highlight on every toolbar item except for the one labeled "Inspect":

    - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
    {
        NSMutableArray *allIdentifiers = [[NSMutableArray alloc] init];
    
        for (NSToolbarItem *toolbarItem in [toolbar items])
        {
            if (![[toolbarItem label] isEqualToString:@"Inspect"])
                [allIdentifiers addObject:[toolbarItem itemIdentifier]];
        }
    
        return [allIdentifiers autorelease];
    }
    

    I cache the allIdentifiers array in an instance variable when I do something like this, so that I only have to do the array construction once.

提交回复
热议问题