【问题标题】:UITableViewCell's subviews can't be roundUITableViewCell 的子视图不能是圆形的
【发布时间】:2016-08-25 03:11:22
【问题描述】:

我想创建一个表格视图,它有一些圆形子视图作为 UIView。我设置了 UIView 的 layer.cornerRadius 和 clipsToBounds。但有些观点并不圆。谁能帮我解决这个问题或给我一些建议?

我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * settCellID = @"settingCellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:settCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settCellID];

        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)];
        view.layer.cornerRadius = 10;
        view.clipsToBounds = 1;
        view.layer.rasterizationScale = [UIScreen mainScreen].scale;
        view.layer.shouldRasterize = 1;
        view.backgroundColor = [UIColor blueColor];
        [cell.contentView addSubview:view];
    }
    return cell;
}

结果:

【问题讨论】:

  • 这是发生在设备上还是模拟器上?
  • 作为行高返回的值是多少。比这个圆形视图的高度大吗?
  • 这是在模拟器中发生的吗?那么有可能是模拟器故障。将模拟器比例设置为 100%(在菜单中:Window -> Scale -> 100%)然后检查。
  • 这发生在模拟器和设备上。我的iphone6s也有这个问题。 @beyowulf
  • 是的,行高是 43.0f @ Anuradh S

标签: ios objective-c uitableview uiview layer


【解决方案1】:

您将尝试使用创建自定义单元类,并创建您的视图而不是使用情节提要以编程方式创建。并在自定义单元格类中设置视图属性。然后实现上面的代码,但我在这里提到了一些变化。

static NSString * settCellID = @"settingCellID";
'CustomClassName' * cell = (CustomClassName*)[tableView dequeueReusableCellWithIdentifier:settCellID];
cell.viewPropertyName.layer.cornerRadius = 10;
cell.viewPropertyName.clipsToBounds = YES;
cell.viewPropertyName.maskToBounds =YES;
cell.viewPropertyName.backgroundColor = [UIColor blueColor];

【讨论】:

  • 感谢您的帮助,我使用您的建议解决了问题。我不知道我们创建圆形视图的方式有什么不同。使用您的方式创建的圆形视图更像是一个 clrcle。如果你知道,请告诉我。
【解决方案2】:

rasterizationScale 和 shouldRasterize 用于“离屏渲染”,但对圆形视图没有影响。并且“view.clipsToBounds = YES”等同于“view.layer.masksToBounds = 1”。我已经使用 CGContextRef 和 UIBezierPath 在 UITableViewCell 上创建了圆形视图,但它们无法创建真正的圆形视图。

【讨论】:

    【解决方案3】:

    在这个 clipsToBound 里面是 Bool 所以你应该给 YES 或 NO 并尝试使用 maskToBound = YES 作为 Corner Radius 如果你想合并下面的视图而不是相应地使用 clipsToBound 而且我不认为 rasterizationScale 和 shouldRasterize 在这里有用.我希望这会对你有所帮助。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString * settCellID = @"settingCellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:settCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settCellID];
    
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)];
        view.layer.cornerRadius = 10;
        view.clipsToBounds = YES;
        view.maskToBounds =YES;
        view.layer.rasterizationScale = [UIScreen mainScreen].scale;
        view.layer.shouldRasterize = 1;
        view.backgroundColor = [UIColor blueColor];
        [cell.contentView addSubview:view];
    }
    return cell;
    }
    

    如果你想 UITableViewCell 圆角和剪辑子视图,而不是关注这个Link Click here

    【讨论】:

    • rasterizationScale 和 shouldRasterize 用于“离屏渲染”,但对圆形视图没有影响。并且“view.clipsToBounds = YES”等同于“view.layer.masksToBounds = 1”。我已经使用 CGContextRef 和 UIBezierPath 在 UITableViewCell 上创建了一个圆形视图,但是它们无法创建一个真正的圆形视图。
    • 然后在情节提要中制作自定义单元格并设置用户定义运行时属性我相信这将帮助您使用用户定义运行时属性喜欢这个链接stackoverflow.com/questions/12301256/…
    • 感谢您的帮助。我已经用 Rock 的方式解决了这个问题。只有使用自定义圆形视图才能解决这个问题。但我不知道它们有什么区别。@Kartik Singh Bhadoriya
    • 我投了 1 票因为你很好地解决了你的问题。 :-)
    猜你喜欢
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 2010-11-22
    • 2014-01-11
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多