NSButton with round corners and background color

前端 未结 8 1983
迷失自我
迷失自我 2021-02-19 06:30

I want a simple push button (the one with the round corners), and to add background to it. I\'ve tried 2 things:

1 - using a round button image: this is working good, un

8条回答
  •  失恋的感觉
    2021-02-19 07:21

    I made a button and succeeded. It looks like this:

    Code:

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        // Drawing code here.
        if let bgColor = bgColor {
            self.layer?.cornerRadius = 4
            self.layer?.masksToBounds = true
            self.layer?.backgroundColor = bgColor.cgColor
            bgColor.setFill()
            NSRectFill(dirtyRect)
        }
    }
    

    drawRect is replaced by draw, and CGColor is replaced by cgColor. The difference between yours and mine is the order. You called super.draw(dirtyRect) as last, and I called it first. Maybe your button looks like this:

    I hope this can solve your problem.

提交回复
热议问题