简体   繁体   English

如何在tableviewcell swift 3添加阴影?

[英]How to add shadow at tableviewcell swift 3?

I want to add shadow at tableviewcell with 4 section (1 tableView with 4 tableViewCell), where the last cell I want to added shadow at 3 sides(left,right,bottom) but in another cell except last cell I just want to added shadow at 2 side(left,right). 我想在tableviewcell上添加4个部分的阴影(1个tableView和4个tableViewCell),其中我想在3个边(左,右,下)添加阴影的最后一个单元格,但是除了最后一个单元格之外的另一个单元格我只想添加阴影在2侧(左,右)。 I write this code in singleton and with extension UIView. 我用单例和扩展名UIView编写这段代码。 this for last cell: 这是最后一个细胞:

 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

}

and this for another cell except last cell: 除了最后一个单元格之外的其他单元格:

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

}

I call this code in cellForRow: 我在cellForRow中调用此代码:

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

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

now this code is work but still not perfect. 现在这段代码工作但仍然不完美。 there is white space between cell. 细胞之间有空白区域。

图片

I just want to make them connected. 我只想让它们连接起来。

Have you tried removing the separator from IB? 您是否尝试从IB中移除分隔符?

在此输入图像描述

Set the Separator to None 分隔符设置为

or another way, set the separator color to translucent 或者换句话说,将分隔符颜色设置为半透明

self.tableView.separatorColor = UIColor.clear

i think you face the problem in dropShadowAtLeftAndRight() in this Line 我认为你在这一行的dropShadowAtLeftAndRight()中面临问题

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

it will return rect looks like this (0.0, 4.0, 375.0, 36.0) in my case. 在我的情况下,它将返回rect看起来像这样(0.0, 4.0, 375.0, 36.0) on top 4px is white space and bottom also 4px white space 在顶部4px是白色空间,底部也是4px白色空间

So your left and right shadow will start from 4 and height is 36. 所以你的左右阴影将从4开始,高度为36。

to solve this issue you need to set like this 要解决这个问题,你需要像这样设置

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM