如何从NSButton中删除灰色边框?

时间:2017-07-18 19:23:08

标签: swift cocoa subclass draw nsbutton

我正在尝试制作一个简单的彩色NSButton。似乎有两个问题我无法解决:

1。 如何在单击时停止突出显示该按钮?我为我的按钮设置了红色背景:

image

点击后,颜色消失:picture

即使单击按钮,我怎么能保持背景颜色?

  1. 我怎么能删除灰色边框?
  2. 我尝试了不同的东西:

        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)
            }
        }
    

    到目前为止,没有任何影响。感谢大家的帮助和想法。

    // UPDATE

    我实现了我的目标,将其添加到代码中:

     let color = NSColor.red
     color.setFill()
     NSRectFill(dirtyRect)
    

    不幸的是,之后,按钮的标题不再显示。那么可能是在NSRectFill之后如何显示按钮标题的问题?

0 个答案:

没有答案
相关问题