HowTo: Customizing Callbacks - e2technologies/ViewCSS GitHub Wiki

Sometimes there is a need to customize an additional element. For example, maybe a button is created that has a custom "bar" at the bottom and you always want that to be the same color as the text. There are 2 ways to do this.

"custom" callback

The "custom" callback is an option on the CSS call. It will call back once the CSS has been applied. Below is an example

class MyButton: UIButton {
	@IBOutlet weak var bar: UIView?
	
	override func awakeFromNib() {
	    super.awakeFromNib()
	    
	    self.css() { (config: ViewCSSConfig) in
	    	self.bar?.backgroundColor = config.color
	    }
	}
	
}

ViewCSSCustomizableProtocol

The "ViewCSSCustomizableProtocol" provides a similar option as above, but uses a protocol instead. Below is an example

class MyButton: UIButton, ViewCSSCustomizableProtocol {
	@IBOutlet weak var bar: UIView?
	
	override func awakeFromNib() {
	    super.awakeFromNib()
	    
	    self.css()
	}
	
	func cssCustomize(object: Any?, class klass: String?, style: String?, config: ViewCSSConfig) {
	    self.bar?.backgroundColor = config.color
	}
	
}

Note that the "cssCustomize" method is called every time ".css" is called. In order to differentiate between the different calls, the "object", "class", and "style" from the ".css" call are included in the callback.