我最近也遇到过这个问题,但我的问题与 macOS 10.14 中的暗模式/亮模式有关。正如其他人所提到的,在 NSPopover 中使用 NSProgressIndicator 时似乎会发生这种情况,但在 Safari 应用程序扩展弹出框内也是如此(我认为这是因为 Safari 弹出框也是您无法直接访问的 NSPopover)。正如horimislime所说,搞乱外观似乎可以解决问题。
我创建了一个 NSProgressIndicator 子类,只有当应用程序在 macOS 10.14 或更高版本中运行时,它才会自动将自己的外观设置为系统所拥有的外观,即使在应用程序运行时它会发生变化。进度指示器在以前的操作系统版本中适用于我,因此如果系统低于 10.14,我将其保持不变。
这是代码:
import Cocoa
class TransparentProgressIndicator: NSProgressIndicator {
override func awakeFromNib() {
super.awakeFromNib()
if #available(OSX 10.14, *) {
// register an observer to detect when the system appearance changes
UserDefaults.standard.addObserver(self, forKeyPath: "AppleInterfaceStyle", options: .new, context: nil)
// set the starting appearance to the one the system has
let light = NSAppearance(named: .aqua)
let dark = NSAppearance(named: .darkAqua)
self.appearance = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark" ? dark : light
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if #available(OSX 10.14, *) {
if keyPath == "AppleInterfaceStyle" {
// set the current appearance to the one the system has
let val: String? = change?[NSKeyValueChangeKey.newKey] as? String
if (val == "Dark") {
self.appearance = NSAppearance(named: .darkAqua)
}
else {
self.appearance = NSAppearance(named: .aqua)
}
}
}
}
deinit {
if #available(OSX 10.14, *) {
UserDefaults.standard.removeObserver(self, forKeyPath: "AppleInterfaceStyle")
}
}
}
这样,进度指示器保持透明,没有奇怪的着色背景,并且仍然根据系统外观更改其配色方案。为了使用它,您需要将此子类的名称添加为界面构建器中的“自定义类”属性:

现在,为了获得问题中要求的自定义背景颜色,您可以只拥有一个包含 NSBox 并为其添加颜色:
