如何在 tableViewCell 周围添加 10px space?

How to add a 10px space around a tableViewCell?

我正在尝试创建一个周围有 10 像素间距的 tableViewCell,这样单元格就不会触及屏幕的边缘。我尝试做这样的事情,我在我的 cellForRowAtIndexPath 中的另一个 Whosebug post 上看到了,但是一旦我在模拟器中 运行 它,我得到的只是黑屏:

 cell.contentView.backgroundColor = UIColor.white

    let whiteRoundedView : UIView = UIView(frame: CGRect(x: 10, y: 10, width: self.view.frame.size.width - 20, height: 188))

    whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.8])
    whiteRoundedView.layer.masksToBounds = false
    whiteRoundedView.layer.cornerRadius = 10

    cell.contentView.addSubview(whiteRoundedView)
    cell.contentView.sendSubview(toBack: whiteRoundedView)

我认为最好从故事板(或 xib)中分配这个 10px。 您可以在您的 UIView 中插入一个前导 space 约束(在您的 tableViewCell 中设计)。 因此,您自动不再使用经常出现问题的框架...

你应该把你的 whiteRoundedView 放在 contentView 的前面。现在 whiteRoundedView 位于单元格的 contentView 后面。

cell.contentView.bringSubview(toFront: whiteRoundedView)