22

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);
Cowirrie
  • 7,218
  • 1
  • 29
  • 42
Narcis
  • 5,862
  • 3
  • 23
  • 30

3 Answers3

62

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

Paul Solt
  • 8,375
  • 5
  • 41
  • 46
  • This one worked perfectly for me, rather than adding an extra cell as many other solutions suggest. Also though you need to unset it if your cells are reused as I only wanted this for the last cell in a table. – simongking Nov 02 '11 at 11:47
  • 1
    The performance tip was a huge help - without the shadowPath table cells are really choppy when they slide. Setting shadowPath is a bit non-obvious and doesn't really appear in any docs that I can find. – The Mad Gamer May 23 '12 at 17:17
  • if I put the shadow on the tableView, not the cell, when scrolling the bound of the tableview changes. How do I update shadowPath to match ? – Luong Huy Duc May 24 '12 at 15:47
  • great answer. I found this very impressive – Saad Mar 19 '14 at 11:23
  • 1
    This does not work when you have a long list of cells! When I scroll down and then up again, cells change their position in viewHierarchy. Newly dequeued cells are not will be added on top which means that these cells hide the shadow effect of the cell which is over it. Any suggestions for that? – blackjacx Jul 01 '16 at 12:13
  • I have a trouble with it. My shadow has wrong position after roll to top http://stackoverflow.com/questions/38325727/cagradientlayer-has-wrong-position-after-scroll-to-top-in-uitableview-swift – Alexander Khitev Jul 12 '16 at 11:28
19

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
shim
  • 9,289
  • 12
  • 69
  • 108
Damasio
  • 496
  • 5
  • 14
2

The view hierarchy of a grouped table view cell is really rather opaque. cell.layer is actually referring to the layer of the main view of the cell, which takes of the entire width of the table. The rounded part of the cell that is inset is actually handled by apple's private methods for drawing grouped cells.

You're probably going to have more luck creating a custom subclass of UITableViewCell.

Jerry Jones
  • 5,393
  • 2
  • 22
  • 14