Ios – How to create shadow for UICollectionViewCell

iosobjective cuicollectionviewuicollectionviewcell

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:

enter image description here

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?

Best Answer

You need to enable the shadow to be created outside of the bounds;

[cell.layer setMasksToBounds:NO];