How to remove gray border from NSButton?

十年热恋 提交于 2019-12-12 05:28:54

问题


I´m trying to make a simple colored NSButton. It seems there are two problems i don´t get solved:

1. How can I stop the button from being highlighted when clicked? I set a red background to my button:

.

When clicked, the color disappears:

How could I keep the backgroundcolor, even when the button is clicked?.

  1. How could I remove the gray border?

I tried different things:

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

Of which nothing has an effect so far. Thank you everyone for help and ideas.

// UPDATE

I´m achieving my goal adding this to the code:

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

Unfortunately after that, the button´s title is no longer shown. So maybe a question of how to show button´s title after NSRectFill ?

来源:https://stackoverflow.com/questions/45175401/how-to-remove-gray-border-from-nsbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!