如何将阴影添加到UITableViewCell的分隔符?

时间:2014-10-26 04:37:03

标签: ios objective-c iphone uitableview

我有一个像这样的UITableView:

enter image description here

我想为每个Cell的分隔符添加一个阴影,结果应如下所示:

enter image description here

我试过了:

cellForRowAtIndexPath:方法中,我添加了以下代码:

cell.layer.shadowOpacity = 1.0;
cell.layer.shadowRadius = 1.7;
cell.layer.shadowColor = [UIColor blackColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0.0, 0.0);

然后它显示如下:

enter image description here

请注意,第一个单元格和状态栏之间存在阴影,这是不需要的。

有关如何删除它的任何想法?它不必构建我正在使用的投影代码。

3 个答案:

答案 0 :(得分:3)

通过将shadowRadius设置为小于shadowOffset y值的值,我能够非常接近您正在寻找的内容。例如,

    cell.layer.shadowOpacity = 1.0
    cell.layer.shadowRadius = 1
    cell.layer.shadowOffset = CGSizeMake(0, 2)
    cell.layer.shadowColor = UIColor.blackColor().CGColor

给了我:

enter image description here

此图片是在拖动表格视图的情况下拍摄的。最顶层的单元格上方没有阴影。 阴影比您要查找的阴影更暗,但您应该可以通过更改shadowOpacity轻松地根据自己的喜好调整阴影。

答案 1 :(得分:2)

在cellForRowAtIndexPath:方法中,

UIBezierPath *shadowPath2 = [UIBezierPath bezierPathWithRect:cell.bounds];
cell.layer.masksToBounds = NO;
cell.layer.shadowColor = [UIColor blackColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
cell.layer.shadowOpacity = 0.5f;
cell.layer.shadowPath = shadowPath2.CGPath;

答案 2 :(得分:0)

对于Swift

cell.layer.shadowOffset = CGSizeMake(0, 2)
cell.layer.shadowColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.7).CGColor
cell.layer.shadowRadius = 1
cell.layer.shadowOpacity = 0.6                
cell.clipsToBounds = false
cell.layer.zPosition = 10
相关问题