Home > Software engineering >  How to set the desired color for the background of the safari app extension
How to set the desired color for the background of the safari app extension

Time:04-20

don't scold me for dumb questions, but I need your help, how much I googled - I could not find anything. I don't understand how to change the background of the main SafariExtensionViewController to the rgb I want?

CodePudding user response:

It is elementary, just make sure you turn wantsLayer on. For example:

override func viewDidLoad() {
    view.wantsLayer = true
    view.layer?.backgroundColor = .black
}

If you are planning to change the color in a NSView inside updateLayer() you'll need to turn wantsUpdateLayer on as well.

class MyView: NSView {
    override var wantsUpdateLayer: Bool { true }
    override func updateLayer() {
        layer?.backgroundColor = NSColor(named: "CustomControlColor")?.cgColor
    }
}

Alternatively you could go without layers like this:

class MyView: NSView {
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        NSColor(named: "CustomControlColor")?.setFill()
        dirtyRect.fill()
    }
}
  • Related