如何通过图层为UITableViewCell实现此边距

时间:2014-03-20 06:02:31

标签: ios objective-c uitableview calayer

我需要为单元格的背景实现这个左边距。

现在我尝试这样做 - 但结果不正确 - 单元格的内容随着边距移动。我该如何解决问题?

CGRect layerFrame = self.layer.frame;
layerFrame.size.width -= kRightLayerMargin;
layerFrame.origin.x += kLeftLayerMargin;
self.layer.frame = layerFrame;

self.layer.backgroundColor = [[SCUtils cellSelectionColor] CGColor];
self.layer.cornerRadius = 10.0f;

enter image description here

1 个答案:

答案 0 :(得分:0)

按如下方式实施单元格的layoutSubviews方法:

- (void)layoutSubviews {
    //have the cell layout normally
    [super layoutSubviews];
    //get the bounding rectangle that defines the position and size of the textLabel
    CGRect textLabelFrame = [[self textLabel] frame];
    //anchor it to the top-left corner
    textLabelFrame.origin = CGPointMake(kLeftLayerMargin, textLabelFrame.origin.y);

    //set the textLabel frame (this will move+resize it)
    [[self textLabel] setFrame:textLabelFrame];

    //reposition the other controls accordingly
}