Neal
Neal

Reputation: 45

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?

Upvotes: 0

Views: 734

Answers (3)

Raj Aryan
Raj Aryan

Reputation: 361

Go to the collectionviewcell.m and add these manually.

I did this to fix it.

- (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;
}

It will add effects and if you want to add some ui then code inside it.

Upvotes: 1

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7434

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

Upvotes: 1

Bista
Bista

Reputation: 7903

Add this line too:

layer.masksToBounds = false

By default it is true and limits the Cell within its frame size, by setting it to false allows cell's shadow to be visible outside its frame.

Upvotes: 0

Related Questions