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

我有一个自定义的UITableViewCell,我想在其中绘制一个垂直分隔符,类似于iOS7中的默认水平分隔符.目前我配置单元格时使用此代码:
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width - rightButtonWidth,1,cell.contentView.bounds.size.height)];
lineView.backgroundColor = [UIColor lightGrayColor];
lineView.autoresizingMask = 0x3f;
[cell.contentView addSubview:lineView];

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

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

解决方法

您的问题是因为如果宽度设置为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和代码.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...