Search code examples
iosobjective-cuitableviewcore-animationcalayer

CALayer properties (ex: drop shadows) not appearing on all UITableViewCells


So I have a UIView in a prototype UITableViewCell. In that view's awakeFromNib method, I have the following code to make a shadow

CALayer *layer = self.layer;

layer.cornerRadius = 5.0f;

// Makes shadow for each cell in all and nearby table views.

CGSize size = self.bounds.size;
CGFloat curlFactor = 15.0f;
CGFloat shadowDepth = 5.0f;

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0.0f, 0.0f)];
[path addLineToPoint:CGPointMake(size.width, 0.0f)];
[path addLineToPoint:CGPointMake(size.width, size.height + shadowDepth)];
[path addCurveToPoint:CGPointMake(0.0f, size.height + shadowDepth)
        controlPoint1:CGPointMake(size.width - curlFactor, size.height + shadowDepth - curlFactor)
        controlPoint2:CGPointMake(curlFactor, size.height + shadowDepth - curlFactor)];

self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.3f;
self.layer.shadowOffset = CGSizeMake(2.0f, 7.0f);
self.layer.shadowRadius = 2.0f;
self.layer.masksToBounds = NO;
self.layer.shadowPath =  path.CGPath; //Sets a path for the shadow. Greatly enhances performance.

The problem is that the shadow only appears on some cells, not all and seems to be random at times. I tried putting the code in viewWillAppear and viewDidAppear but it didn't work all together in there either. Does anyone have a clue of what can possibly be causing this?


Solution

  • I finally realized that the problem is that the drop shadows are not missing, they were just overlapped by other cells.

    So this answer can solve the problem (you'll need sendSubviewToBack: in this case).

    FYI, the new UICollectionView in iOS 6 will give you more control over cell z-indices, and for pre-iOS 6 support, PSTCollectionView is a nice alternative.