3

有没有办法告诉 OS X 自动设置样式/着色NSToolbarItem

我通过 IB/Xcode 添加了一个“图像工具栏项”,并将图标设置为黑色 PDF ,如文档中所述

但是,我的结果与 App Store 中的结果不同:

在此处输入图像描述

我正在寻找类似于默认情况下 iOS 中的 TabBar 所做的事情。

我是 OS X 开发的新手......所以任何指导都会被应用!

4

2 回答 2

7

图像需要制作模板才能获得正确的样式(例如雕刻和蓝色样式)。

这可以在代码中使用-[NSImage setTemplate:] 通过让您的图像名称以“模板”结尾(不需要更改代码)来完成。


要特别获得蓝色样式,您必须将无边框 NSButton 设置为工具栏项的自定义视图(而不是标准项)。该按钮必须具有一种类型,使其显示其状态(例如,圆形纹理切换按钮),并且当它具有打开状态时,它将获得蓝色样式。

于 2014-07-07T21:11:14.083 回答
0

如果您尝试在代码中创建一个有色工具栏项,我就是这样做的。创建正确类型的按钮NSButtonTypeToggle,然后设置按钮属性,然后将按钮添加到工具栏项的自定义视图,最后返回工具栏项。

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSToolbarItemIdentifier)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {

    // create toolbar items
    NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
    toolbarItem.label = @"title";
    NSImage *iconImage = [NSImage imageNamed:NSImageNameColumnViewTemplate];
    NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 40.0, 40.0)];
    button.title = @"";
    button.image = iconImage;
    [button setButtonType:NSButtonTypeToggle];
    button.bezelStyle = NSBezelStyleTexturedRounded;
    button.action = @selector(toggleColumnView:);
    [toolbarItem setView:button];
    return toolbarItem; 
}
于 2020-04-14T04:10:09.313 回答