bolizhou
bolizhou

Reputation: 1218

Shadow Issue for UITableViewCell

I have a custom UITableViewCell and I wrote shadow code like this in its layoutSubviews method:

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 4.0);
    self.layer.shadowRadius = 4.0;
    self.layer.shadowOpacity = 1.0;
}

But when I run it, instead of having shadow on the edge, all the subviews on that cell got shadows, like the screen shot below, which is not what I was expecting.

enter image description here

I tried self.contentView.layer but then there wasn't any shadows at all.

So what do I do to have the shadows just on the bottom edge of the cell as usual? I don't want to shadow the subviews.

UPDATE:

I added all the subviews on my cell with [cell addSubview:xxx]; directly, so they are not on contentView of the cell.

Upvotes: 0

Views: 162

Answers (2)

BSB
BSB

Reputation: 277

Please try below code:

#import <QuartzCore/QuartzCore.h>

Add below code in cellForRowAtIndexPath

{
    tblCustomCell.layer.shadowColor = [[UIColor darkGrayColor] CGColor];
    tblCustomCell.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
    tblCustomCell.layer.shadowRadius = 3.0f;
    tblCustomCell.layer.shadowOpacity = 1.0f;
}

Upvotes: 1

Rahul Shirphule
Rahul Shirphule

Reputation: 999

try this one

instead of setting shadow in layoutsubView set it in cellForRowAtIndexPath

cell.layer.shadowColor = [UIColor blackColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0, 4.0);
cell.layer.shadowRadius = 4.0;
cell.layer.shadowOpacity = 1.0;

Upvotes: 1

Related Questions