Following is the collection view delegate function that I am using to render items with gradient view.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
collectionView.layoutIfNeeded()
let collectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell
let gradient = CAGradientLayer()
gradient.frame = collectionView.bounds
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
if indexPath.row % 2 == 0 {
gradient.colors = [UIColor(hex: 0xD74F9B).cgColor, UIColor(hex: 0xF1828A).cgColor]
} else {
gradient.colors = [UIColor(hex: 0x5368D3).cgColor, UIColor(hex: 0x4AAAD1).cgColor]
}
collectionViewCell.cardView.layer.insertSublayer(gradient, at: 0)
return collectionViewCell
}
When the collection view is loaded, I get the following view, in which one of the collection view items gets clipped.
However, as I scroll the collection view, I get the properly rendered view.
How can this issue be fixed?