【问题标题】:ios13 Dark Mode change not recognized by tableview Cell?ios13暗模式更改不被tableview Cell识别?
【发布时间】:2020-02-07 06:14:52
【问题描述】:

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

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

如果应用以深色模式启动,单元格也会显示正确的深色背景。如果应用程序处于后台时模式更改,则单元格背景颜色不会更改。单元格标签正确切换颜色。

对于表格视图单元格,我使用以下函数进行渐变:

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)

任何想法,为什么在应用程序处于活动状态或后台时发生模式更改后,只有渐变函数似乎无法获得正确的颜色?

问候

【问题讨论】:

  • 在该函数中添加图层会导致图层重复。仅在之前未添加的情况下添加图层。
  • 我之前遇到过这个问题,我发布了一个带有问题链接的答案。如果您有任何其他问题,请查看并告诉我。

标签: ios swift uicolor ios-darkmode


【解决方案1】:

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

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

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

More description here

【讨论】:

  • 这必须在 uitableviewcell 中完成(最初添加和替换)?无法添加正确大小的渐变层(self.bounds、self.contentView.bound 和 self.backgroundView.bounds 似乎小于显示宽度)
  • 是的。 layoutSubviews 是您需要重写的函数,以便在大小发生变化时重新计算大小。
【解决方案2】:

如果这个渐变在这种类型的每个单元格上,那么它应该只是单元格的一部分,而不是由包含的视图控制器插入。然后,在您的单元格中,您可以实现:


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

  if traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle {
    // reload the gradient layer to react
  }
}

你也可以在你的视图控制器中实现它并重新加载数据,但它更混乱。

【讨论】:

  • 你能否最终指出如何在 uitableviewcell 中添加渐变层并确保它只添加一次?不知道这个问题,因为所有教程和文档都告诉我将它添加到 uitableview 类中。
猜你喜欢
  • 2020-02-14
  • 2019-11-15
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多