【问题标题】:How to set a corner radius for a custom cell如何为自定义单元格设置角半径
【发布时间】:2016-02-12 05:23:55
【问题描述】:

这是我的示例输出,在下面的详细信息中有三个单元格,我为每个自定义单元格设置了圆角半径。但它无法正常工作。第一个单元格的圆角半径不起作用,第二个单元格的顶部右上和左上正在工作,请说一下该怎么做

   -(void)layoutSubviews
     {
[super layoutSubviews];
self.backgroundColor = [UIColor clearColor];
self.contentView.layer.cornerRadius =5;
self.contentView.layer.masksToBounds = YES;

 }

我已将上面的代码应用到我的单元类文件中

【问题讨论】:

标签: ios uitableview xcode6


【解决方案1】:

在 cellForRowAtIndexPath 中使用

    cell.layer.cornerRadius=5

如果您在不同部分有不同的单元格,请确保为所有部分编写此代码

【讨论】:

    【解决方案2】:

    其他简单选项在 Storyboard 或 XIB 中设置 RuntimeAttributs。

    首先选择您的视图(单击您的视图)> 转到 Identiti Inspector(在您的 Xcode 右侧的属性检查器附近)> 用户定义的运行时属性 > + 单击并一个一个地添加下面两个在这个领域。

    layer.cornerRadius
    layer.masksToBounds
    

    清理并构建您的项目。无需编写任何 .h 或 .m 文件。

    【讨论】:

    • 通过 Storyboard 实现这一点的好方法。为我工作! (我还将cornerRadius设置为8)
    【解决方案3】:

    对我来说效果很好,也许我的代码对某人有帮助。

    NSInteger rowsAmount = [tableView numberOfRowsInSection:indexPath.row];
    if (rowsAmount) {
    
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:( UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];
    
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.view.bounds;
        maskLayer.path  = maskPath.CGPath;
    
        cell.layer.mask = maskLayer;
    
    }
    

    【讨论】:

      【解决方案4】:

      这是一个老问题,但我想添加我的解决方案。如果有人在这个问题上挣扎未来。 按照 dev_m 在他的评论中的建议,我将代码写入 willDisplayCell()。

      那你能不能在表视图委托方法 tableView:willDisplayCell:forRowAtIndexPath: method cell.backgroundColor = [UIColor clearColor]; cell.contentView.layer.cornerRadius = 5; cell.contentView.layer.masksToBounds = YES;

      但我在上面添加了一个 layoutIfNeeded 属性。所以我的代码是:

      func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
          cell.layoutIfNeeded()
          let leaveCell = cell as! LeaveItem
          leaveCell.vContainerRoot?.applyShadowWithCornerRadius(opacity: 0.5, radius: 20, edge: AIEdge.All, shadowSpace: 0.1)
          leaveCell.vLeaveItemHeader?.roundCorners(corners: [.topLeft, .topRight], radius: 20)
      }
      

      applyShadowWithCornerRadius 和 roundCorners 方法是我的扩展函数。

      【讨论】:

        【解决方案5】:

        awakeFromNib:()试试这个代码

        self.backgroundColor = [UIColor clearColor];
        self.contentView.layer.cornerRadius =5;
        self.contentView.layer.masksToBounds = YES;
        

        【讨论】:

        • 那你能不能在表视图委托方法tableView:willDisplayCell:forRowAtIndexPath: method cell.backgroundColor = [UIColor clearColor]; cell.contentView.layer.cornerRadius = 5; cell.contentView.layer.masksToBounds = YES;
        【解决方案6】:

        这是一种不同的方法:在返回单元格之前的 cellForRow 中,您可以尝试以下代码:

        CGFloat corner = 20.0f;
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cell.backgroundView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(corner, corner)];
        CAShapeLayer  *shapeLayer = (CAShapeLayer *)cell.backgroundView.layer;
        shapeLayer.path = path.CGPath;
        shapeLayer.fillColor = cell.textLabel.backgroundColor.CGColor;
        shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
        shapeLayer.lineWidth = 1.0f;
        

        告诉我!

        【讨论】:

        【解决方案7】:

        斯威夫特 4:

        override func layoutSubviews() {
            super.layoutSubviews()
        
            self.contentView.clipsToBounds = true
            self.contentView.layer.cornerRadius = 5
        
        }
        

        【讨论】:

          猜你喜欢
          • 2016-06-14
          • 2021-03-02
          • 2016-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多