2

I create a collectionView and I want to add shadow to every cell, I wrote this code

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyCollectionViewCell
        cell.myLabel.text = self.items[indexPath.item]
        cell.backgroundColor = UIColor.yellow
        cell.layer.shadowColor = UIColor.black.cgColor
        cell.layer.shadowOffset = CGSize(width: 3, height: 3)
        cell.layer.shadowOpacity = 0.7
        cell.layer.shadowRadius = 4.0
        return cell
    }

but this is the output

enter image description here

I want it be like this UIView with rounded corners and drop shadow?

Community
  • 1
  • 1
amjad
  • 145
  • 1
  • 12

1 Answers1

9

Your code will probably work if you also say:

cell.layer.masksToBounds = false

The reason is that a shadow is outside the layer's bounds, so if we are masking to the bounds, you won't see the shadow.

matt
  • 515,959
  • 87
  • 875
  • 1,141