3

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

MadMaxAPP
  • 1,035
  • 2
  • 16
  • 39

2 Answers2

13

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

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • and this has to be done (adding inititally and replacing) in uitableviewcell? Can not make it to have the gradientlayer added with correct size (self.bounds, self.contentView.bound and also self.backgroundView.bounds seem to be smaller than display width) – MadMaxAPP Oct 13 '19 at 20:42
  • Yep. `layoutSubviews` is the function you need to override to recalculate the size whenever it changes. – Mojtaba Hosseini Oct 13 '19 at 20:44
0

If this gradient is on every cell of this type, then it should simply be a part of the cell, not inserted by the containing view controller. Then, in your cell, you can implement:


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

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

You could also implement this in your view controller and reload data but it's messier.

Procrastin8
  • 4,193
  • 12
  • 25
  • Can you eventually point me how to add the gradient layer in uitableviewcell and make sure it's only added once? Didn't know about this problem as all tutorials and documentation told me to add it in uitableview class. – MadMaxAPP Oct 13 '19 at 20:40