Appearance proxies / UI_APPEARANCE_SELECTOR in Swift?

Klaas picture Klaas · Oct 3, 2014 · Viewed 8k times · Source

The Apple documentation states:

To participate in the appearance proxy API, tag your appearance property selectors in your header with UI_APPEARANCE_SELECTOR.

In Objective-C one can annotate properties with UI_APPEARANCE_SELECTOR like this:

@property (nonatomic, strong) UIColor *foregroundColor UI_APPEARANCE_SELECTOR;

How can I do the same in Swift?

Answer

Yoichi Tagaya picture Yoichi Tagaya · Feb 26, 2015

Mark your custom view property as dynamic.

For example:

class YourCustomView: UIView {
    @objc dynamic var subviewColor: UIColor? {
        get { return self.yourSubview.backgroundColor }
        set { self.yourSubview.backgroundColor = newValue }
    }
    ...
}

Then:

YourCustomView.appearance().subviewColor = UIColor.greenColor()