Highlight NSToolbarItems

假如想象 提交于 2019-12-19 02:27:12

问题


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

highlight http://a2.s3.p.quickshareit.com/files/screenshot_b28b67ba9411513d6.png

Is there an easy way? If not, tell me the difficult one. =)


回答1:


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.




回答2:


If you made your toolbar in Interface Builder, you can click on the individual NSToolbarItems and check the Selectable box in the Inspector for the ones you want to have that look. No code needed.




回答3:


See Selectable Toolbar Items in the Cocoa documentation.



来源:https://stackoverflow.com/questions/566793/highlight-nstoolbaritems

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!