【问题标题】:How to mimic UITableView iOS7 default line separator style?如何模仿 UITableView iOS7 默认行分隔符样式?
【发布时间】:2013-12-04 03:57:42
【问题描述】:

我有一个自定义的 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 像素宽度呈现。我尝试将宽度设置为 0.5 磅,但这条线根本没有渲染。

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

【问题讨论】:

  • 您是否尝试将调整大小掩码设置为 0(即不调整大小)?
  • 现在试过了,没什么区别。
  • 颜色可以使用 UIColor colorWithRed:green:blue:alpha 方法

标签: ios uitableview ios7


【解决方案1】:

这里是 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
            }
        }
    }
}

【讨论】:

    【解决方案2】:

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

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

    然后直接应用到线视图上。

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

    结果与默认的UITableView's 分隔符没有太大区别。

    【讨论】:

    • 这听起来是最简单最好的解决方案。
    【解决方案3】:

    您的问题是因为如果宽度设置为 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;
                }
            }
        }
    }
    

    上面的代码 sn-p 将检查哪个维度(宽度或高度)小于或等于 1,并根据屏幕分辨率调整其大小。此代码也适用于自动布局(已测试)。

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

    【讨论】:

    • 我在 SO! 上看到的最聪明的答案之一!谢谢!
    【解决方案4】:

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

    在你的代码 sn-p 中你应该添加这一行:

    lineView.clipsToBounds = YES;
    

    【讨论】:

    • 感谢您的建议,我尝试在 IB 中添加一个视图,将宽度设置为 1。问题是线条仍然比默认分隔符更粗。
    • 也许最好用一张图片来做。
    猜你喜欢
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2011-09-20
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多