ios13表格模式单元格无法识别暗模式更改? [英] ios13 Dark Mode change not recognized by tableview Cell?

查看:84
本文介绍了ios13表格模式单元格无法识别暗模式更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查现有应用程序是否可以与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)

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

致谢

解决方案

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

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

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

此处有更多描述

I'm checking my existing app to work correctly with the new introduced dark mode feature of ios 13.

Everything seems to work fine, only the cell background in one of my tableViews is not refreshed according the mode (dark / light).

If the app starts in dark mode, the cells also show the correct dark background. If the mode changes while the app is in background, the cell background color is not changed. The cell label switches the color correct.

for the tableview cells i use the following func for a gradient:

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
}

i add the gradient background to the table cells in

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

with the following code

cell.layer.insertSublayer(gradient(frame: cell.bounds), at: 0)

Any idea, why only the gradient func does not seem to get the correct colors after a mode change happened while app active or in background?

Regards

解决方案

Cell will detect, layer will not! You must manually update all layer adaptations in the cell for example.

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

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

More description here

这篇关于ios13表格模式单元格无法识别暗模式更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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