If there's one thing the controls we've talked about so far have in common, it is that they can be graphically customized with the use of simple functions. This is a great a feature that has been added to UIKit since iOS5, and it allows us to modify the looks of any default control with just a few lines of code.
This feature is improved by the UIAppearance
protocol that, thanks to the appearance proxy, forwards the customizations to all the instances of a specific class. The appearance proxy for a class can be retrieved using the appearance
function, and as it returns instancetype
, its properties can be easily accessed. Here is an example of the code needed to set onTintColor
for all the UISwitch
instances of an application:
UISwitch.appearance().onTintColor = UIColor.redColor()
The properties that take part in the UIAppearance
protocol and can therefore be modified through the appearance proxy must be marked with the UI_APPEARANCE_SELECTOR
tag. This portion of the...