I have a UIView
and I set a shadowPath
for it like this:
func addShadow() {
let cornerRadius: CGFloat = self.containerView.frame.height/2
self.containerView.layer.shadowPath = UIBezierPath(roundedRect: self.containerView.frame, cornerRadius: cornerRadius).cgPath
self.containerView.layer.shadowRadius = cornerRadius
self.containerView.layer.shadowOffset = .zero
self.containerView.layer.shadowOpacity = 0.2
self.containerView.layer.cornerRadius = cornerRadius
self.containerView.layer.shadowColor = UIColor(named: "whiteColor")?.cgColor
}
And this is whiteColor
:
And now my problem is
When I change the appearance of the phone, the
shadowPath
's color doesn't change automatically. What should I do to make the color dynamic?
All layer properties don't react to color changes automatically (due to the conversion to CGColor
).
Instead, react to trait collection changes and re-set the properties when the color appearance changes:
override open func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 13, *), self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
// re-set your properties here
}
}