如何在tableViewCell周围添加10px空间?

时间:2016-10-04 21:52:07

标签: ios swift uitableview tableviewcell

我试图创建一个围绕它的间距为10px的tableViewCell,这样单元格就不会碰到屏幕的边缘。我试着在我的cellForRowAtIndexPath中的另一个stackOverflow帖子上看到这样的事情,但是一旦我在模拟器中运行它,我得到的就是黑屏:

 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)

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您应该将whiteRoundedView带到contentView的前面。现在whiteRoundedView位于单元格的contentView。

之后
cell.contentView.bringSubview(toFront: whiteRoundedView)
相关问题