UIView的阴影路径颜色不变 [英] UIView's shadowpath color doesn't change

查看:80
本文介绍了UIView的阴影路径颜色不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView,并为此设置了shadowPath:

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
}

这是whiteColor:

现在我的问题是

当我更改手机的外观时,shadowsPath color不会自动更改.我该怎么做才能使颜色动态?

When I change the appearance of the phone the shadowsPath color doesn't change automatically. What should I do to make the color dynamic?

推荐答案

所有图层属性都不会自动响应颜色变化(由于转换为CGColor).

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?) {
    if #available(iOS 13, *), self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
        // re-set your properties here
    }
}

这篇关于UIView的阴影路径颜色不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆