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