Swift 如何从NSButton中删除灰色边框?
我想做一个简单的彩色按钮。似乎有两个问题我没有解决: 一,。 单击时,如何阻止按钮高亮显示?我为我的按钮设置了红色背景: 单击时,颜色将消失: 我怎样才能保持背景色,即使在点击按钮的时候Swift 如何从NSButton中删除灰色边框?,swift,cocoa,subclass,draw,nsbutton,Swift,Cocoa,Subclass,Draw,Nsbutton,我想做一个简单的彩色按钮。似乎有两个问题我没有解决: 一,。 单击时,如何阻止按钮高亮显示?我为我的按钮设置了红色背景: 单击时,颜色将消失: 我怎样才能保持背景色,即使在点击按钮的时候 我怎样才能去掉灰色的边框 我尝试了不同的方法: class myButton: NSButton { override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) self.w
class myButton: NSButton {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
self.wantsLayer = true
self.isBordered = false
self.title = "hello"
self.setButtonType(NSMomentaryLightButton)
self.layer?.cornerRadius = 0
self.layer?.borderWidth = 0
self.layer?.masksToBounds = false
self.layer?.backgroundColor = NSColor.red.cgColor
self.appearance = NSAppearance(named: NSAppearanceNameAqua)
}
}
到目前为止还没有任何影响。谢谢大家的帮助和想法
//更新
我正在实现我的目标,将此添加到代码中:
let color = NSColor.red
color.setFill()
NSRectFill(dirtyRect)
不幸的是,在此之后,按钮的标题不再显示。因此,可能有一个问题是如何在NSRectFill之后显示按钮的标题?使用NSTextField并为其设置单击手势,而不是使用NSButton。@这将是一个非常简单的解决方案!还在想办法解决这个问题。@ElTomato很棒的解决方案!非常感谢你!您可能已经尝试了TPButton类。。。这回答了你的问题吗?