Adding rounded corner and drop shadow to UICollectionViewCell Adding rounded corner and drop shadow to UICollectionViewCell ios ios

Adding rounded corner and drop shadow to UICollectionViewCell


Neither of these solutions worked for me. If you place all your subviews into the UICollectionViewCell content view, which you probably are, you can set the shadow on the cell's layer and the border on the contentView's layer to achieve both results.

cell.contentView.layer.cornerRadius = 2.0f;cell.contentView.layer.borderWidth = 1.0f;cell.contentView.layer.borderColor = [UIColor clearColor].CGColor;cell.contentView.layer.masksToBounds = YES;cell.layer.shadowColor = [UIColor blackColor].CGColor;cell.layer.shadowOffset = CGSizeMake(0, 2.0f);cell.layer.shadowRadius = 2.0f;cell.layer.shadowOpacity = 0.5f;cell.layer.masksToBounds = NO;cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath;

Swift 3.0

self.contentView.layer.cornerRadius = 2.0self.contentView.layer.borderWidth = 1.0self.contentView.layer.borderColor = UIColor.clear.cgColorself.contentView.layer.masksToBounds = trueself.layer.shadowColor = UIColor.black.cgColorself.layer.shadowOffset = CGSize(width: 0, height: 2.0)self.layer.shadowRadius = 2.0self.layer.shadowOpacity = 0.5self.layer.masksToBounds = falseself.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.contentView.layer.cornerRadius).cgPath


Swift 3 version:

cell.contentView.layer.cornerRadius = 10cell.contentView.layer.borderWidth = 1.0cell.contentView.layer.borderColor = UIColor.clear.cgColorcell.contentView.layer.masksToBounds = truecell.layer.shadowColor = UIColor.gray.cgColorcell.layer.shadowOffset = CGSize(width: 0, height: 2.0)cell.layer.shadowRadius = 2.0cell.layer.shadowOpacity = 1.0cell.layer.masksToBounds = falsecell.layer.shadowPath = UIBezierPath(roundedRect:cell.bounds, cornerRadius:cell.contentView.layer.cornerRadius).cgPath


In case it helps: Here is the swift for rounding the corners:

cell.layer.cornerRadius = 10cell.layer.masksToBounds = true

with cell being a variable controlling the cell: often, you will use this in override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

Enjoy!