Okay, here is what I did when I needed to change the look of a button: subclass it.
// // LSButtonColor.swift // // Created by Lloyd Sargent on 8/26/16. // Copyright © 2016 Canna Software. All rights reserved. // License: Permission is hereby granted, free of charge, to any // person obtaining a copy of this software and associated // documentation files (the “Software”), to deal in the // Software without restriction, including without // limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software // is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice // shall be included in all copies or substantial portions // of the Software. // // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // import Cocoa class LSButtonColor: NSButton { fileprivate struct AssociatedKeys { static var variableDictionary = "ls_variableDictionary" } fileprivate var variableDictionary: [String:AnyObject]! { get { var localVariableDictionary = objc_getAssociatedObject(self, &AssociatedKeys.variableDictionary) as! [String:AnyObject]! if localVariableDictionary == nil { localVariableDictionary = [String:AnyObject]() objc_setAssociatedObject(self, &AssociatedKeys.variableDictionary, localVariableDictionary, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } return localVariableDictionary } set(updatedDictionary) { objc_setAssociatedObject(self, &AssociatedKeys.variableDictionary, updatedDictionary, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } var backgroundColor: NSColor! { get { return variableDictionary["backgroundColor"] as? NSColor } set(newBackgroundColor) { var localVariableDictionary = variableDictionary localVariableDictionary?["backgroundColor"] = newBackgroundColor variableDictionary = localVariableDictionary } } var altBackgroundColor: NSColor! { get { return variableDictionary["altBackgroundColor"] as? NSColor } set(newAltBackgroundColor) { var localVariableDictionary = variableDictionary localVariableDictionary?["altBackgroundColor"] = newAltBackgroundColor variableDictionary = localVariableDictionary } } var borderColor: NSColor! { get { return variableDictionary["borderColor"] as? NSColor } set(newBorderColor) { var localVariableDictionary = variableDictionary localVariableDictionary?["borderColor"] = newBorderColor variableDictionary = localVariableDictionary self.layer?.borderColor = newBorderColor.cgColor } } var cornerRadius: CGFloat { get { return variableDictionary["cornerRadius"] as! CGFloat } set(newCornerRadius) { var localVariableDictionary = variableDictionary localVariableDictionary?["cornerRadius"] = newCornerRadius as AnyObject? variableDictionary = localVariableDictionary self.layer?.cornerRadius = newCornerRadius } } var borderWidth: CGFloat { get { return variableDictionary["borderWidth"] as! CGFloat } set(newBorderWidth) { var localVariableDictionary = variableDictionary localVariableDictionary?["borderWidth"] = newBorderWidth as AnyObject? variableDictionary = localVariableDictionary self.layer?.borderWidth = newBorderWidth } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. if self.isHighlighted { if self.layer != nil { self.layer?.backgroundColor = self.altBackgroundColor.cgColor } } else { if self.layer != nil { self.layer?.backgroundColor = self.backgroundColor.cgColor } } } }
Okay, the first thing you might be puzzling over is the objc_getAssociatedObject - originally I did this as a category and it is an easy-peasy way of adding variables to categorys (everyone says you can’t do it, but everyone doesn’t realize that, yes, you can - it’s just not obvious).
Unfortunately, I ran into some issues, so I just made it a class - and didn’t bother to take out the associated object (why mess with success?)
Anyway, you should be able to pull out the relevant code to change the background of any button.