I have the following cellForRow
code
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SquareCVC", for: indexPath) as! SquareCVC
if let gradient1 = items[indexPath.row].hex_up, let gradient2 = items[indexPath.row].hex_down{
cell.contentView.setGradientBackground(firstColor: gradient1, secondColor: gradient2)
}else{
cell.contentView.setGradientBackground(firstColor: "#EFEFF1", secondColor: "#EFEFF1")
}
But when I scroll collection view, background color changes by itself There is my setGradientBackground function
func setGradientBackground(firstColor: String, secondColor: String) {
let colorTop = colorWithHexString(hexString: firstColor).cgColor
let colorBottom = colorWithHexString(hexString: secondColor).cgColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorTop, colorBottom]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.frame = self.bounds
self.layer.insertSublayer(gradientLayer, at:0)
}
Any ideas?