NSButton设置背景色macOS 10.12 [英] NSButton set background color macOS 10.12

查看:192
本文介绍了NSButton设置背景色macOS 10.12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在情节提要中创建了NSButtons,我想为其更改背景色.我已经搜索了SO和Google,但是没有一个解决方案真正适用于我的项目甚至是一个简单的测试项目.我尝试过:

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. 设置button.layer.backgroundColor(包括有无) 导入QuartzCore并设置wantsLayer).
  2. 从NSButton获取NSButtonCell并更改背景颜色.我已尝试使用带边框,无边框,透明等方式进行此操作.背景颜色没有变化.我尝试过从正方形更改为渐变,没有运气.
  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.

推荐答案

这必须是macOS中的错误.我可以通过使用带有一个EMPTY覆盖的EMPTY类进行子类化,使NSButtonCell轻松响应backgroundColor:

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!

这篇关于NSButton设置背景色macOS 10.12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆