2017-04-18 131 views
2

我想添加阴影在tableviewcell与4节(1 tableView与4 tableViewCell),最后一个单元格我想添加三边(左,右,底部)阴影,但在另一个单元格,但最后一个单元格,我只是想在左侧(左侧,右侧)添加阴影。我用singleton和扩展UIView编写这段代码。 此为最后一个单元格:如何在tableviewcell swift 3添加阴影?

func dropShadowAtBottom() { 

    self.layer.masksToBounds = false 
    self.layer.shadowColor = UIColor.black.cgColor 
    self.layer.shadowOpacity = 0.2 
    self.layer.opacity = 0.3 
    self.layer.shadowOffset = CGSize(width: 0, height: 2) 
    self.layer.shadowRadius = 2 
    self.clipsToBounds = false 

} 

这对于除最后一个小区中其他小区:

func dropShadowAtLeftAndRight() { 

    self.layer.masksToBounds = false 
    self.layer.shadowColor = UIColor.black.cgColor 
    self.layer.shadowOpacity = 0.5 
    self.layer.shadowOffset = CGSize(width: 0, height: 0) 
    self.layer.shadowRadius = 2 
    let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 4); // inset top/bottom 
    self.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath 

    self.layer.rasterizationScale = UIScreen.main.scale 
    self.layer.shouldRasterize = true 

} 

我把这个代码在cellForRow:

if(indexPath.row == currentOrderHistory.listOrderItems.count - 1){ 
      cell.bodyView.dropShadowAtBottomOnly() 

     }else{ 
      cell.bodyView.dropShadowAtLeftAndRight() 
     } 
     return cell 

现在这个代码的工作,但仍不完美。细胞之间有白色空间。

picture

我只想让他们连接。

+2

如果您发布想要实现的图像 –

+0

试图分享您想要实现的内容 –

+0

我会添加图片。请klik的“图片”文字 –

回答

0

您是否尝试从IB中删除分隔符?

enter image description here

设置的分离

或另一种方式,设置分隔符颜色半透明

self.tableView.separatorColor = UIColor.clear 
+0

谢谢,但它没有任何变化 –

0

我想你在这一行面临的问题dropShadowAtLeftAndRight()

let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 4); 

它会返回rect看起来像这样(0.0, 4.0, 375.0, 36.0)在我的情况。在上面4PX是白色的空间,底部也4PX空白

所以,你的左边和右边的阴影从400开始,高度36

来解决这个问题,你需要这样设置

let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 0); 
+0

是的,但如果我改变dy为0,所有单元格将有底部的影子 –

+0

那么你只能使用“dropShadowAtBottom”方法,并在你的单元格左右添加两个视图给左和右洒下。 –