9

I am trying to add shadow to my custom UICollectionViewCell, This is the code I am using in my custom collection view cell class:

self.layer.shadowOffset = CGSizeMake(1, 0);
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = .25;

This is giving shadow to the components of the collection view cell.

shim
  • 9,289
  • 12
  • 69
  • 108
Avinash Sharma
  • 665
  • 1
  • 7
  • 23

4 Answers4

18

Don't forget to add these 2 lines

self.clipsToBounds = false      
self.layer.masksToBounds = false
Vinu David Jose
  • 2,569
  • 1
  • 21
  • 38
8

Swift 4.2 and xcode 10

here's code for adding a shadow.

    cell.contentView.layer.cornerRadius = 2.0
    cell.contentView.layer.borderWidth = 1.0
    cell.contentView.layer.borderColor = UIColor.clear.cgColor
    cell.contentView.layer.masksToBounds = true

    cell.layer.backgroundColor = UIColor.white.cgColor
    cell.layer.shadowColor = UIColor.gray.cgColor
    cell.layer.shadowOffset = CGSize(width: 0, height: 2.0)//CGSizeMake(0, 2.0);
    cell.layer.shadowRadius = 2.0
    cell.layer.shadowOpacity = 1.0
    cell.layer.masksToBounds = false
    cell.layer.shadowPath = UIBezierPath(roundedRect:cell.bounds, cornerRadius:cell.contentView.layer.cornerRadius).cgPath
Anil Gupta
  • 1,155
  • 1
  • 19
  • 26
4

cell.layer.backgroundColor should not be clear color

try

cell.layer.backgroundColor = UIColor.white.cgColor
Bobo Shone
  • 721
  • 6
  • 16
0

Go to CustomCollectionViewCell.m file Worked for me. Hope it helps...

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //////// make shadow  of total view
        self.clipsToBounds = NO;
        self.layer.masksToBounds = NO;
        self.layer.shadowRadius = 5;
        self.layer.shadowOpacity = 0.5;
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, 1);
        self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;

        // make radius of the cell
        self.layer.cornerRadius = 5;

    }
    return self;
}
Raj Aryan
  • 363
  • 2
  • 15