使用 NSTintConfiguration 自定义侧边栏

问题描述

Adopt the new look of macOS video 中,您可以使用 NSTintConfiguration 使用此代码自定义侧边栏图标颜色。

func outlineView(_ outlineView: NSOutlineView,tintConfigurationForItem item: Any) -> NSTintConfiguration? {
  if case let sectionItem as SectionItem = item {
    /* 
       This outline view uses a type called "SectionItem" to populate its top-level sections.
       Here we choose a tint configuration based on a hypothetical `isSecondarySection` property on that type.
     */
    return sectionItem.isSecondarySection ? .monochrome : .default
  }
  // For all other cases,a return value of `nil` indicates that the item should inherit a tint from its parent.
  return nil
}

当我试图在我的应用侧边栏上强制使用颜色时,例如像这样的例子是黄色。

func outlineView(_ outlineView: NSOutlineView,tintConfigurationForItem item: Any) -> NSTintConfiguration? {
    return .init(fixedColor: .systemYellow)
}

它(有点)按预期工作,这意味着未选择的任何行都按预期自定义,但是当您选择一行时,SF 符号会改变颜色。我希望它是黄色的。

这就是它的样子。您可以看到图标色调确实是黄色,但前提是您不选择它们。

enter image description here

我想要的是突出显示的行色调颜色与未选择的行相同。就像 Apple 在其应用中的做法一样。

a screenshot of Apple Mail.app

解决方法

您看到绿色强调色行选择突出显示 (NSTableRowView.emphasized = true) 和白色图标,因为您的大纲视图当前是窗口的第一响应者。如果您在大纲视图之外选择不同的控件,例如窗口主要内容区域中的 NSTextField,则行选择将更改为非活动外观 (emphasized = false)。这独立于新的色调配置 API。

Mail 和 Finder 会采取一些措施来防止它们的侧边栏大纲视图在大多数情况下成为第一响应者,因此它们通常具有与您的第二个屏幕截图类似的非活动外观。如果您需要更细粒度的控制,您可以通过在大纲视图上设置 refusesFirstResponder = true 或通过子类化 NSOutlineView 并覆盖 acceptsFirstResponder 来近似此行为。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...