Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITableViewCell layer shadow

Tags:

I'm trying to add a shadow to a UITableViewCell using the layer.shadowColor, Offset, Radius but it doesn't seem to affect it in any way. The table is grouped style. Any ideas why?

Here is the code i'm using:

cell.layer.shadowColor= [UIColor blackColor].CGColor; cell.layer.shadowRadius = 5.0; cell.layer.shadowOffset = CGSizeMake(10, 10); 
like image 291
Narcis Avatar asked Aug 23 '10 10:08

Narcis


People also ask

How to add shadow to UICollectionViewCell?

Shadows on a UICollectionViewCell Adding a drop shadow to a UICollectionViewCell follows the same method as adding a drop shadow to a UIView . Configure the shadowRadius , shadowOpacity , shadowColor , and shadowOffset of the view's layer property to the desired shadow design.

What is the purpose of IOS shadow?

Layers can have a shadow. The layer's shadow uses several elements: shadowOffset , shadowColor , shadowOpacity , and shadowRadius . Each element changes the respective appearance. You can offset the shadow differently to change the direction the shadow is cast - how far from the layer and in which direction.


2 Answers

You need to also set the shadow opacity, it defaults to 0 and you won't see anything if you don't explicitly set it.

CALayer Reference

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

Also note, that if you don't set the shadow path you will have terrible performance on the iPhone/iPad. Use something like the following code to set a shadow path, it removes the need to blur the layers underneath your tableviewcell's to create a "high quality" shadow.

CGRect shadowFrame = cell.layer.bounds; CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath; cell.layer.shadowPath = shadowPath; 

Watch video 425 (also 424 and 426) to learn more about shadows from the WWDC 2010 Videos available here: WWDC 2010 Session Videos

like image 199
Paul Solt Avatar answered Sep 18 '22 15:09

Paul Solt


Just adding the @Paul Soult answer in Swift:

cell?.layer.shadowOffset = CGSizeMake(0, 1) cell?.layer.shadowColor = UIColor.blackColor().CGColor cell?.layer.shadowRadius = 1 cell?.layer.shadowOpacity = 0.6  // Maybe just me, but I had to add it to work: cell?.clipsToBounds = false  let shadowFrame: CGRect = (cell?.layer.bounds)! let shadowPath: CGPathRef = UIBezierPath(rect: shadowFrame).CGPath cell?.layer.shadowPath = shadowPath 
like image 43
Damasio Avatar answered Sep 21 '22 15:09

Damasio