NSButton 不屏蔽边界

NSButton not masking to bounds

我正在将 iOS 的应用程序转移到 OSX 中的工具栏应用程序。尽管我使用的是 swift,但对象却大不相同。然而,我遇到了一个我似乎无法克服的主要问题。

下面的代码生成了所示的 NSButton,但我无法摆脱灰色背景。 masktobound 无效。我试过单独遮盖每个角,没有效果。我只想要一个简单的圆形按钮。

我也有几个带圆角的按钮,但它们也显示浅灰色背景。

有什么指点吗?示例代码将不胜感激。

let connectButton = NSButton.init(title: NSLocalizedString(" ", comment: "OnButtonAccessibility"), target: self, action: #selector(toggle))
connectButton.wantsLayer = true
connectButton.isBordered = false
connectButton.layer?.masksToBounds=true
if #available(OSX 10.13, *) {
  connectButton.layer?.maskedCorners=[.layerMaxXMaxYCorner]
} else {

connectButton.layer?.backgroundColor = NSColor.blue.cgColor
connectButton.layer?.cornerRadius=80
connectButton.layer?.borderColor=DarkerBlue.cgColor
connectButton.layer?.borderWidth=3
(connectButton.cell as! NSButtonCell).isBordered=false
(connectButton.cell as! NSButtonCell).backgroundColor=NSColor.clear
connectButton.isTransparent=true
connectButton.frame=CGRect(x: 80, y: self.view.frame.size.height-260, width: 160, height: 160)
connectButton.tag=1002
self.view.addSubview(connectButton)

看来你至少要触摸NSButton.cell才能让它符合按钮样式。使用的 backgroundstyle 似乎没有什么区别是你有按钮填充或包含图像。

connectButton.cell?.backgroundStyle=NSView.BackgroundStyle.dark