【问题标题】:Automatic Styling/Tinting of NSToolbarItemNSToolbarItem 的自动样式/着色
【发布时间】:2014-07-06 19:30:03
【问题描述】:

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

我通过 IB/Xcode 添加了一个“图像工具栏项”,并将图标设置为黑色 PDF as described in the documentation

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

我正在寻找类似于 iOS 中 TabBar 的默认功能。

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

【问题讨论】:

    标签: macos cocoa nstoolbar nstoolbaritem


    【解决方案1】:

    为了获得正确的样式(例如雕刻和蓝色样式),需要将图像制作成模板。

    这可以在带有-[NSImage setTemplate:] 的代码中完成,方法是让您的图像名称以“模板”结尾(无需更改代码)。


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

    【讨论】:

    • 按照你的建议使用了 NSButtons - 就像一个魅力。谢谢!
    • 太棒了!我试图复制 Xcode 的显示/隐藏项目导航器/实用程序等,这成功了。
    • 在“渲染”下拉菜单中选择“模板”本身不会执行任何操作;图片名称确实需要以“模板”结尾。
    【解决方案2】:

    如果您尝试在代码中创建着色工具栏项,我就是这样做的。创建正确类型的按钮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; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 2016-09-29
      • 2016-10-25
      相关资源
      最近更新 更多