Neal Neal - 2 months ago 28
Swift Question

UICollectionViewCell can't set shadow

I want set shadow with

UICollectionViewCell
, like this:

img

I write code in Custom Cell

override func awakeFromNib() {
super.awakeFromNib()
layer.shadowColor = UIColor(red: 0.7176470757, green: 0.7176470757, blue: 0.7176470757, alpha: 1.0000000000).CGColor
layer.shadowOffset = CGSizeMake(0, 4)
layer.shadowRadius = 2
layer.shadowOpacity = 1
}


but can't set cell shadow. All subview by set shadow:

mycell

How can I solve this problem?

Answer

You should put clipsToBounds.That is the problem.

self.layer.shadowColor = UIColor(red: 0.7176470757, green: 0.7176470757, blue: 0.7176470757, alpha: 1.0000000000).CGColor
self.layer.shadowOffset = CGSizeMake(0, 4)
self.layer.shadowOpacity = 1.0
self.layer.shadowRadius = 2.0
self.clipsToBounds = false
self.layer.masksToBounds = false