ios - UIView的阴影路径颜色不变

标签 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
}


这是whiteColor

enter image description here

现在我的问题是


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

最佳答案

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

相反,对特征收集更改做出反应,并在颜色外观更改时重新设置属性:

override open func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    if #available(iOS 13, *), self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
        // re-set your properties here
    }
}

关于ios - UIView的阴影路径颜色不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58847977/

相关文章:

ios - 为现有对象添加扩展名的 Swift 文件命名的最佳实践是什么?

ios - Firestore : Is it possibile to have duplicate auto generated ID across different subcollection?

ios - 蓝牙连接至 iPhone 的设备

ios - 无法使用类型为 'append' 的参数列表调用 '(String)',其中每个变量都是非可选的

iphone - CGAffineTransform Scale 在设备旋转后修改

ios - 使用 prepareForSegue 和使用 Core Data 传递数据有什么区别?

iphone - 如何在 xcode 中的某个特定间隔后在圆形进度 View 中添加多种颜色?

ios - 在 Swift 2.2 中将 Int64 转换为 Float

iphone - 使用 NSKeyedArchiver 复制 UIView 抛出 NSInvalidUnarchiveOperationException

iPhone UIViewController 中的 UIPickerView