I have created a simple custom UIView:
final class TestView: UIView {
var testColor: UIColor = .white {
didSet {
backgroundColor = testColor
}
}
}
Then I wrote this in my view controller:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var testView: TestView!
@IBOutlet weak var testView2: TestView!
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
TestView.appearance().testColor = .red
}
}
}
By doing this, I get an error:
Could you help me understanding what's wrong here and how to implement the UIAppearance proxy for any custom UIView?
Thank you for your help

You need to make the property
@objcanddynamic:Worth noting: the
UIAppearanceproxy does NOT affect views which are already part of the view hierarchy.So, in your example, adding
@objc dynamicto your property will get rid of the crash, but you will not see any change to the two@IBOutletviews.If you call it as part of
viewDidLoad()(instead ofDispatchQueue.main.asyncAfter):The two
@IBOutletviews will get the red background.Or, if you add a new view to the hierarchy, it will get the red background: