ios - UIView的shadowpath颜色没有改变

标签 ios swift uiview ios-darkmode shadowpath

我有一个 UIView 并为其设置了一个 shadowPath ,如下所示:

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
}

这是白色:

enter image description here

现在我的问题是

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?

最佳答案

所有图层属性不会自动对颜色变化使用react(由于转换为 CGColor)。

相反,当颜色外观发生变化时,对特征集合更改使用react并重新设置属性:

override open func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)

    if #available(iOS 13, *), self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
        // re-set your properties here
    }
}

关于ios - UIView的shadowpath颜色没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58583349/

相关文章:

ios - Objective-C 用不同的 UIView 替换当前的 UIView

ios - 从未调用过 NSNumber 的 RKValueTransformer

ios - Cordova 构建 ios 给出空错误消息

iOS Swift 2 - 删除 tableView 行时出错

iOS - 运行 Swift 单元测试时找不到 'MyProject-Swift.h' 文件

ios - UILabel 的宽度超过使用编程自动布局的文本

ios - 两个 View 层次结构之间的公共(public) subview

ios - NSURLConnection 检索 JSON

ios - XLForms 中未触发的事件

swift - 如何用静态方法重新实例化一个类?