I want to add a shadow to my CollectionViewCell, but the following code doesn't work.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! myCell // Configure the cell cell.layer.cornerRadius = 15.0 cell.layer.masksToBounds = true cell.layer.borderWidth = 0.0 cell.backgroundView?.layer.shadowColor = UIColor.black.cgColor cell.backgroundView?.layer.shadowRadius = 5 cell.backgroundView?.layer.shadowOpacity = 1 cell.backgroundView?.layer.shadowOffset = CGSize(width: 0, height: 0) return cell }
I also tried:
cell.layer.shadowColor = UIColor.green.cgColor cell.layer.shadowRadius = 5 cell.layer.shadowOpacity = 1 cell.layer.shadowOffset = CGSize(width: 0, height: 0)
I don't get an error, but no shadow occurs.
As far as I tried, with adding shadow settings directly to the `layer` of the cell, I could see the shadow:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyCell // Configure the cell cell.layer.cornerRadius = 15.0 cell.layer.borderWidth = 0.0 cell.layer.shadowColor = UIColor.black.cgColor cell.layer.shadowOffset = CGSize(width: 0, height: 0) cell.layer.shadowRadius = 5.0 cell.layer.shadowOpacity = 1 cell.layer.masksToBounds = false //<- return cell }
Please do not forget to set false to `cell.layer.masksToBounds`.