如何模仿UITableView iOS7默认行分隔符样式?

时间:2013-11-20 08:50:13

标签: ios uitableview ios7

我有一个自定义的UITableViewCell,我想在其中绘制一个垂直分隔符,类似于iOS7中的默认水平分隔符。目前,我在配置单元格时使用此代码:

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width - rightButtonWidth, 0, 1, cell.contentView.bounds.size.height)];
lineView.backgroundColor = [UIColor lightGrayColor];
lineView.autoresizingMask = 0x3f;
[cell.contentView addSubview:lineView];

如图所示,默认分隔符以1像素高度渲染,而我的宽度为2像素。我尝试将宽度设置为.5点,但是根本没有渲染线。

颜色也是关闭的,显然不是lightGrayColorUIColor中的颜色常量是否匹配?编辑:颜色为RGB 207,207,210,似乎没有在UIColor.h中列出。

Cell with vertical separator

4 个答案:

答案 0 :(得分:16)

您的问题是因为如果宽度设置为1px,视图的视网膜宽度将为2px。我建议创建一个UIView的子类,让我们称之为CustomDivider,然后在-layoutSubviews中你会做这样的事情:

-(void)layoutSubviews {
    [super layoutSubviews];
    if([self.constraints count] == 0) {
        CGFloat width = self.frame.size.width;
        CGFloat height = self.frame.size.height;
        if(width == 1) {
            width = width / [UIScreen mainScreen].scale;
        }
        if (height == 0) {
            height = 1 / [UIScreen mainScreen].scale;
        }

        if(height == 1) {
            height = height / [UIScreen mainScreen].scale;
        }
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, width, height);
    }
    else {
        for(NSLayoutConstraint *constraint in self.constraints) {
            if((constraint.firstAttribute == NSLayoutAttributeWidth || constraint.firstAttribute == NSLayoutAttributeHeight) && constraint.constant == 1) {
                constraint.constant /=[UIScreen mainScreen].scale;
            }
        }
    }
}

上面的代码片段将检查哪个尺寸(宽度或高度)小于或等于1,它将根据屏幕分辨率调整大小。此代码也适用于autolayout(已测试)。

这种方法适用于IB和代码。

答案 1 :(得分:6)

像@danypata一样子类工作正常,但我的情况是修复现有代码快速简单。可替代地,

- (CGFloat)defaultOnePixelConversion
{
     return 1.f / [UIScreen mainScreen].scale;
}

然后直接应用于线视图。

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, /*1*/[self defaultOnePixelConversion], height);

结果与默认UITableView's分隔符差别不大。

答案 2 :(得分:0)

如果您使用自定义UITableViewCells,您可以通过他们的.xib来完成。只需添加一个具有所需颜色的UIView,将其设置为一个像素并将其放置在您想要的位置。无需以编程方式执行此操作。

在您的代码段中,您应该添加以下行:

lineView.clipsToBounds = YES;

答案 3 :(得分:0)

这是swift 4版本:

override func layoutSubviews() {
    super.layoutSubviews()
    if self.constraints.count == 0 {
        var width = self.frame.size.width
        var height = self.frame.size.height
        if width == 1{
            width = width / UIScreen.main.scale

        }
        if height == 0{
            height = 1 / UIScreen.main.scale
        }
        if height == 1 {
            height = height / UIScreen.main.scale
        }
        self.frame = CGRect(x:self.frame.origin.x, y:self.frame.origin.y, width:width, height:height);
    }
    else{
        for constraint:NSLayoutConstraint in self.constraints{
            if(constraint.firstAttribute == NSLayoutAttribute.width || constraint.firstAttribute == NSLayoutAttribute.height) && constraint.constant == 1{
                constraint.constant /= UIScreen.main.scale
            }
        }
    }
}