UITableViewCell 的子视图不能是圆形的

Posted

技术标签:

【中文标题】UITableViewCell 的子视图不能是圆形的【英文标题】:UITableViewCell's subviews can't be round 【发布时间】: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 【参考方案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 上创建了一个圆形视图,但是它们无法创建一个真正的圆形视图。 然后在情节提要中制作自定义单元格并设置用户定义运行时属性我相信这将帮助您使用用户定义运行时属性喜欢这个链接***.com/questions/12301256/… 感谢您的帮助。我已经用 Rock 的方式解决了这个问题。只有使用自定义圆形视图才能解决这个问题。但我不知道它们有什么区别。@Kartik Singh Bhadoriya 我投了 1 票因为你很好地解决了你的问题。 :-)

以上是关于UITableViewCell 的子视图不能是圆形的的主要内容,如果未能解决你的问题,请参考以下文章

按 UITableViewCell 时,圆形视图消失了

圆形图像视图最初在 UITableViewCell 中加载为正方形 - Xamarin.iOS

更改 UITableViewCell 中圆形编辑按钮的颜色

为啥我必须删除为 UITableViewCell 添加的子视图,而不是在 UITableViewCell 的子类中?

将大于 cellHeight 的子视图添加到 UITableViewCell?

将 UITableViewCell 的子视图放在最前面