How to remove gray border from NSButton?

2019-08-25 01:52发布

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:

image.

When clicked, the color disappears: picture

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 ?

0条回答
登录 后发表回答