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

poj*_*ojo 7 uitableview ios 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];
Run Code Online (Sandbox Code Playgroud)

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

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

细胞与垂直分离器

dan*_*ata 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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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


Yoo*_*Lee 7

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

- (CGFloat)defaultOnePixelConversion
{
     return 1.f / [UIScreen mainScreen].scale;
}
Run Code Online (Sandbox Code Playgroud)

然后直接应用于线视图.

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

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