NSButton设置背景颜色macOS 10.12

问题描述:

我在故事板中创建了NSButtons,我想更改背景颜色。我已经搜索过SO和Google,但没有任何解决方案实际上可以用于我的项目或甚至是一个简单的测试项目。我已经试过:NSButton设置背景颜色macOS 10.12

  1. 设置button.layer.backgroundColor(包括有和没有 进口QuartzCore和设置wantsLayer)。
  2. 从NSButton获取NSButtonCell并更改背景颜色。我已经尝试了这种有边框,无边框,透明等。没有改变背景颜色。我试过从正方形变为渐变,没有运气。
+0

不知道我明白这个问题,但我需要一个可调整大小的矩形按钮。我尝试了几种类型,正方形,渐变... – Tim

这一定是macOS中的一个bug。我可以让NSButtonCell到的backgroundColor回应简单地通过一个空的类继承一个空覆盖:

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

当使用这个子类,它所有的作品如预期!