Swift 使用CALayer修改NSButton的外观在macOS 10.12和10.10中的工作方式不同

Swift 使用CALayer修改NSButton的外观在macOS 10.12和10.10中的工作方式不同,swift,cocoa,calayer,layer,nsbutton,Swift,Cocoa,Calayer,Layer,Nsbutton,我正在尝试更改应用程序中NSButton的外观。按钮为“方形”和“瞬时推入” 代码是: let layer = CALayer() toggleTimespanButton.wantsLayer = true toggleTimespanButton.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.onSetNeedsDisplay layer.backgroundColor = NSColor.white.cgColor

我正在尝试更改应用程序中NSButton的外观。按钮为“方形”和“瞬时推入”

代码是:

let layer = CALayer()
toggleTimespanButton.wantsLayer = true
toggleTimespanButton.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.onSetNeedsDisplay
layer.backgroundColor = NSColor.white.cgColor
layer.cornerRadius = (toggleTimespanButton.bounds.size.width)/2
layer.masksToBounds = true
layer.borderWidth = 1
layer.borderColor = appDelegate.mainColor.cgColor
toggleTimespanButton.layer = layer
在macOS 10.12中,该按钮看起来不错:

但在macOS 10.10中,按钮标题不可见。一切看起来都很好,包括白色的按钮和粉红色的边框等

但为什么按钮标题在macOS 10.10中不可见


我已尝试逐个禁用所有行,只有当图层未添加到按钮时,文本才会重新显示…

您是否尝试将NSView的
wantsLayer
属性设置为
true
?抱歉,我在创建问题时忘了粘贴它。我现在已经将它添加到我发布的代码片段中。但问题仍然存在,按钮标题是不可见的……只是想澄清一下:按钮标题在macOS 10.10中是不可见的,在macOS 10.12中看起来很好。这发生在我身上,在10.10和10.11中都有一个只显示图像的按钮。一旦找到解决方案,我就会发布。我必须实现层的委托的
绘制(u:in:)
,并将其中的
层内容显式设置为图像,使其显示在每个操作系统版本上。不确定这对您的情况是否有帮助,但这可能是一个很好的起点。