简体   繁体   English

NSButton不掩盖边界

[英]NSButton not masking to bounds

I am transferring an app for iOS to a toolbar app in OSX. 我正在将iOS的应用程序转移到OSX中的工具栏应用程序。 Although I am using swift, the objects are rather different. 尽管我使用的是Swift,但对象却大不相同。 I am having one major problem, however, that I cannot seem to overcome. 但是,我有一个主要问题,我似乎无法克服。

The code below produces the NSButton shown, but I cannot get rid of the grey background. 下面的代码生成所示的NSButton,但是我无法摆脱灰色背景。 masktobound has no effect. masktobound无效。 I have tried masking each corner individually, no effect. 我尝试过分别遮盖每个角,没有效果。 I just want a simple round button. 我只想要一个简单的圆形按钮。

I also have several buttons with rounded corners, however these also show the light grey background. 我也有几个带有圆角的按钮,但是这些按钮也显示浅灰色背景。

Any pointers? 有指针吗? Sample code would be appreciated. 示例代码将不胜感激。

圆形按钮,代码如下

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)

It appears that you must at least touch the NSButton.cell to have it conform to the button style. 看来您至少必须触摸NSButton.cell才能使其符合按钮样式。 The backgroundstyle used seems to make no difference is you have the button filled or containing an image. 所使用的backgroundstyle似乎没有什么不同,因为您已将按钮填充或包含图像。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM