1

NSButtons在 Storyboard 中创建了我想更改背景颜色的内容。我搜索了 SO 和 Google,但没有一个解决方案真正适用于我的项目,甚至没有一个简单的测试项目。我试过了:

  1. 设置 button.layer.backgroundColor (包括导入和不导入 QuartzCore 和设置wantsLayer)。
  2. 从 NSButton 获取 NSButtonCell 并更改背景颜色。我已经尝试过有边框、无边框、透明等。背景颜色没有变化。我试过从正方形变成渐变,没有运气。
4

1 回答 1

2

这一定是 macOS 中的一个错误。我可以使 NSButtonCell 响应 backgroundColor 只需使用一个 EMPTY 类进行子类化,并使用一个 EMPTY 覆盖:

class ColorButtonCell: NSButtonCell {
    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
        super.draw(withFrame: cellFrame, in: controlView)
    }
}

使用这个子类时,一切都按预期工作!

于 2016-11-13T18:21:03.050 回答