且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

UITableViewCell:圆角和阴影

更新时间:2021-07-14 22:07:23

这个问题来得正是时候!我真的只是自己解决了同样的问题.

This question comes at a good time! I literally JUST solved this same issue myself.

  1. 在单元格的内容视图中创建一个 UIView(我们将其称为 mainBackground).这将包含您单元格的所有内容.在故事板中放置它并应用必要的约束.
  2. 创建另一个UIView.这将是带有阴影的那个(让我们将其称为 shadowLayer).完全按照您在 mainBackground 中所做的放置它,但在它后面,并应用相同的约束.
  3. 现在您应该可以如下设置圆角和阴影:

  1. Create a UIView (let's refer to it as mainBackground) inside your cell's Content View. This will contain all of your cell's content. Position it and apply necessary constraints in the Storyboard.
  2. Create another UIView. This one will be the one with the shadow (let's refer to it as shadowLayer). Position it exactly as you did mainBackground, but behind it, and apply the same constraints.
  3. Now you should be able to set the rounded corners and the shadows as follows:

cell.mainBackground.layer.cornerRadius = 8  
cell.mainBackground.layer.masksToBounds = true

cell.shadowLayer.layer.masksToBounds = false
cell.shadowLayer.layer.shadowOffset = CGSizeMake(0, 0)
cell.shadowLayer.layer.shadowColor = UIColor.blackColor().CGColor
cell.shadowLayer.layer.shadowOpacity = 0.23
cell.shadowLayer.layer.shadowRadius = 4

然而,这里的问题是:计算每个单元格的阴影是一项缓慢的任务.当您滚动表格时,您会注意到一些严重的滞后.解决此问题的***方法是为阴影定义一个 UIBezierPath,然后对其进行光栅化.所以你可能想要这样做:

However, the problem here is: calculating the shadow for every single cell is a slow task. You'll notice some serious lag when you scroll through your table. The best way to fix this is to define a UIBezierPath for the shadow, then rasterize it. So you may want to do this:

cell.shadowLayer.layer.shadowPath = UIBezierPath(roundedRect: cell.shadowLayer.bounds, byRoundingCorners: .AllCorners, cornerRadii: CGSize(width: 8, height: 8)).CGPath
cell.shadowLayer.layer.shouldRasterize = true
cell.shadowLayer.layer.rasterizationScale = UIScreen.mainScreen().scale

但这又产生了一个新问题!UIBezierPath 的形状取决于 shadowLayer 的边界,但是在调用 cellForRowAtIndexPath 时边界没有正确设置.因此,您需要根据 shadowLayer 的边界调整 shadowPath.***的方法是将 UIView 子类化,并在 bounds 属性中添加一个属性观察器.然后在 didSet 中设置阴影的所有属性.请记住更改故事板中 shadowLayer 的类以匹配您的新子类.

But this creates a new problem! The shape of the UIBezierPath depends on shadowLayer's bounds, but the bounds are not properly set by the time cellForRowAtIndexPath is called. So, you need to adjust the shadowPath based on shadowLayer's bounds. The best way to do this is to subclass UIView, and add a property observer to the bounds property. Then set all the properties for the shadow in didSet. Remember to change the class of your shadowLayer in the storyboard to match your new subclass.

class ShadowView: UIView {
    override var bounds: CGRect {
        didSet {
            setupShadow()
        }
    }

    private func setupShadow() {
        self.layer.cornerRadius = 8
        self.layer.shadowOffset = CGSize(width: 0, height: 3)
        self.layer.shadowRadius = 3
        self.layer.shadowOpacity = 0.3
        self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 8, height: 8)).cgPath
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.main.scale
    }
}