Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell top shadow is covered by UITableView header view

I have a UITableView with a custom HeaderView (added via storyboard). I want to put a drop shadow on the first cell in the table view. Unfortunately, the header view covers the shadow.

I am making the shadow in cellForRowAtIndexPath with:

[cell setClipsToBounds:NO];
[cell.layer setMasksToBounds:NO];
[cell.layer setShadowOffset:CGSizeMake(0, 2)];
[cell.layer setShadowColor:[[UIColor blackColor] CGColor]];
[cell.layer setShadowRadius:4.0];
[cell.layer setShadowOpacity:1.0];
[cell.layer setZPosition:10.0];

The shadow appears if I set the hidden state of the header view to YES. If the header if visible, it covers my shadow. I need the shadow to display in front of the header view.

I have tried:

[self.tableView sendSubviewToBack:self.headerView];

Which has no effect.

What's the correct way to accomplish this? Thanks!

like image 491
istan Avatar asked Dec 12 '25 11:12

istan


1 Answers

Setting the header view's zPosition to negative works for me:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.tableHeaderView.layer.zPosition = -1;
}

I didn't need to modify view ordering or set the zPosition of cells.

like image 157
Timothy Moose Avatar answered Dec 15 '25 16:12

Timothy Moose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!