NSButton set background color macOS 10.12

可紊 提交于 2019-12-11 06:38:17

问题


I have NSButtons created in Storyboard that I would like to change the background color for. I have searched SO and Google but none of the solutions actually work on my project or even a simple test project. I've tried:

  1. Setting the button.layer.backgroundColor (including with and without importing QuartzCore and setting wantsLayer).
  2. Getting the NSButtonCell from the NSButton and changing the background color. I've tried this with bordered, unbordered, transparent, etc. No change in background color. I've tried changing from square to gradient, no luck.

回答1:


This must be a bug in macOS. I can make the NSButtonCell respond to backgroundColor simply by subclassing with an EMPTY class with one EMPTY override:

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

When using this subclass, it all works as expected!



来源:https://stackoverflow.com/questions/40576392/nsbutton-set-background-color-macos-10-12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!