I need to create a shadow for the cells inside a UICollectionView
. I've subclassed the cells and inside the layoutSubviews
I've added the following code:
-(void)layoutSubviews{
[super layoutSubviews];
self.layer.masksToBounds = NO;
self.layer.shadowOpacity = 0.75f;
self.layer.shadowRadius = 5.0f;
self.layer.shadowOffset = CGSizeZero;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
}
But the cells gets higher and this is the result:
If I remove the:
self.layer.masksToBounds = NO;
The cells are shown correctly (with a distance of 10px between them) but the shadow is not visible. What am I doing wrong? Also, is it correct to add the shadow inside the layoutSubviews
method?
You need to enable the shadow to be created outside of the bounds;
[cell.layer setMasksToBounds:NO];
func dropShadow() {
self.layer.masksToBounds = false
self.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
self.layer.shadowOpacity = 0.5
self.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
self.layer.shadowRadius = 4.0
self.layer.cornerRadius = 5.0
}
//Direct Add Shadow to cell
Cell.dropShadow()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With