首页 > 解决方案 > 无法在自定义表格视图单元格中使 ImageView 成为一个圆圈

问题描述

我正在尝试为表格视图中的每个自定义单元格显示一个圆形图像。我已经在改变它的cornerRadius,但由于某种原因,图像仍然会完全显示——即使 ImageView 的边框已经设置为圆形。

这是一段代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> CustomTableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "celda", for: indexPath) as! CustomTableViewCell

        cell.imgView.layer.borderWidth = 1
        cell.imgView.layer.borderColor = UIColor.black.cgColor
        cell.imgView.layer.cornerRadius = cell.imgView.frame.height/2

        //...the rest of function
        //image is added to imgView eventually
}

这是sim中的结果

我尝试将代码放在单元格文件中的 override func awakeFromNib 中,但结果相同。

欢迎任何建议:)

标签: iosuitableviewtableview

解决方案


请按照以下步骤尝试此代码:

步骤1:

使高度和宽度相同image view。添加1:1 ratioin storyboard 以image view获得相等的高度和宽度。

第2步:

cell.imgView.layer.cornerRadius = cell.imgView.frame.height/2
cell.imgView.clipsToBounds = true

它可能会帮助你。谢谢。


推荐阅读