iPhone UITableViewCell layer shadow

前端 未结 3 1584
忘了有多久
忘了有多久 2020-12-07 19:03

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 id

相关标签:
3条回答
  • 2020-12-07 19:46

    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

    0 讨论(0)
  • 2020-12-07 19:52

    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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题