ios - ios13表格模式单元格无法识别暗模式更改?

标签 ios swift uicolor ios-darkmode

我正在检查我现有的应用程序是否可以与ios 13的新引入的暗模式功能一起正常使用。

一切似乎都正常,只有一个tableViews中的单元格背景没有根据模式(暗/亮)刷新。

如果应用程序在黑暗模式下启动,则单元格还会显示正确的黑暗背景。如果在应用程序处于后台状态下更改模式,则单元格背景颜色不会更改。单元格标签可正确切换颜色。

对于tableview单元格,我使用以下func进行渐变:

func gradient(frame:CGRect) -> CAGradientLayer { 

    let gradColor1 = UIColor(named: "gradientBright")!
    let gradColor2 = UIColor(named: "gradientDark")!

    let layer = CAGradientLayer()
    layer.frame = frame
    layer.startPoint = CGPoint(x: 0.5, y: 0)
    layer.endPoint = CGPoint(x: 0.5, y: 1)
    layer.colors = [
        gradColor1.cgColor,
        gradColor2.cgColor
    ]
    layer.shadowOpacity = 0.7
    layer.shadowRadius = 10.0
    return layer
}

我将渐变背景添加到表格单元格中
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

用下面的代码
cell.layer.insertSublayer(gradient(frame: cell.bounds), at: 0)

任何想法,为什么在应用程序处于 Activity 状态或在后台运行模式更改后,仅渐变功能似乎无法获得正确的颜色?

问候

最佳答案

Cell将检测到,layer将不会检测到!例如,您必须手动更新layer中的所有cell改编。

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

    if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
        removeAndReaddGradientIfNeeded()
    }
}

More description here

关于ios - ios13表格模式单元格无法识别暗模式更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58312095/

相关文章:

ios - 将 Facebook SDK 与 Xcode Swift 项目集成 - AppDelegate 错误?

ios - 桥接 header 中导入的文件数量是否会影响编译时间?

ios - 基于uicolor的渐变不起作用

objective-c - UIColor colorWithRed :green:blue: always produces black 颜色

ios - 检查客户端浏览器是否使用 rails 响应 URL

ios - 如何将 FMDB 导入 swift 框架

ios - 使用类中的项目填充 UIPickerView

ios - 如何绘制(_ rect : CGRect) actually work?

ios - UISlider 跟踪标注未与拇指对齐

iphone - 为什么这里使用 "error:&error"(objective-c)