Reputation: 23
I have a custom TableViewCell on my TableViewController. The cell has a UIView that is turned into a gradient that displays black at the bottom and clear (transparent) color on the top of the UIView.
It looks fine when the app loads, but as I scroll, the gradient turns more and more black and the 'fading' itself becomes smaller and gets closer to the top of the UIView.
I do not know how to make it stay as it is when the app loads at first.
Here's my code:
let gradient = CAGradientLayer()
gradient.frame = cell.gradientView.bounds
let colour:UIColor = .black
gradient.colors = [colour.withAlphaComponent(0.0).cgColor,colour.cgColor]
cell.gradientView.layer.insertSublayer(gradient, at: 0)
Upvotes: 0
Views: 116
Reputation: 100503
Add this code in awakeFromNib not in cellForRow , as because of dequeuing the gradient is being added multiple times to the cell
let gradient = CAGradientLayer()
gradient.frame = cell.gradientView.bounds
let colour:UIColor = .black
gradient.colors = [colour.withAlphaComponent(0.0).cgColor,colour.cgColor]
self.gradientView.layer.insertSublayer(gradient, at: 0)
Upvotes: 1