ios - 无法在自定义表格视图单元格中使 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
}
我尝试将代码放在单元格文件中的 override func awakeFromNib 中,但结果相同。
欢迎任何建议:)
解决方案
请按照以下步骤尝试此代码:
步骤1:
使高度和宽度相同image view
。添加1:1 ratio
in storyboard 以image view
获得相等的高度和宽度。
第2步:
cell.imgView.layer.cornerRadius = cell.imgView.frame.height/2
cell.imgView.clipsToBounds = true
它可能会帮助你。谢谢。
推荐阅读
- sql - 如何更改列以接受某种特定格式的值?
- vue.js - Vue if 语句处理得不够快
- delphi - Delphi,使用什么控件代替多对复选框和组合框?
- sql - 链接到多个相关记录的语法
- c++ - gcc 5.4前后的std::accumulate声明
- bash - bash 脚本能否在不杀死它可能已经启动的任何应用程序的情况下终止?
- django - 如何在拍卖应用程序中处理并发出价?
- python - cython中并行函数的运行时间
- java - 为什么 applicationContext.getBean(beanName) 明显存在时返回 null?
- php - 我如何处理和限制在 laravel 项目上执行的查询数量?