如何在表视图单元格内的imageview上设置cornerRadius

时间:2016-05-10 11:37:15

标签: ios swift

我创建了一个自定义的表格视图单元格,并在该单元格上添加了一个UIImageView。我想让图像视图为圆形,请参阅下面的代码。存在一个问题,当表第一次显示时,circularImageView.bounds值不正确,这使得图像不是圆形的。刷新表格视图后,图像看起来正确。如何处理第一次加载表视图?

class CircularTableViewCell: UITableViewCell {
   @IBOutlet weak var circularImageView: UIImageView!

   override func layoutSubviews() {
      circularImageView.layer.cornerRadius =      
         circularImageView.bounds.height / 2
      circularImageView.clipsToBounds = true
      circularImageView.layer.masksToBounds = true
   }

我试图在cellForRowAtIndexpath上设置此值。但是第一次显示此单元格时,circularImageView.bounds.height不受约束

8 个答案:

答案 0 :(得分:2)

// ============================================= =============

您有两个选择:

1)您可以在UITableViewCellClass内部使用此函数:

override func draw(_ rect: CGRect) {
    super.draw(rect)
    circularImageView.layer.cornerRadius = circularImageView.frame.width / 2
}

2)您可以添加固定宽度(例如50),并在awakeFromNib函数中执行以下操作:

circularImageView.layer.cornerRadius = 50 / 2

// ============================================= =============

答案 1 :(得分:1)

您可以使用cellForRowAtIndexpath方法设置转角半径,也可以使用自定义单元格类的awakeFromNib方法,该方法是UITableViewCell的子类。

希望这会有所帮助:)

答案 2 :(得分:1)

在awakeFromNib方法中设置角半径。

override func awakeFromNib() {
 super.awakeFromNib()

  circularImageView.layer.cornerRadius = circularImageView.bounds.height / 2

  circularImageView.clipsToBounds = true

}

答案 3 :(得分:0)

图像应该与您创建圆形图像的高度和宽度相等。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellIdentifier", forIndexPath: indexPath) as! CustomCellIdentifier
cell.yourImageView.layer.cornerRadius = cell.yourImageView.frame.size.height /2;
cell.yourImageView.layer.clipToBounds = true;
return cell;

}

我认为这对你有帮助。如果你有任何问题,请告诉我。

答案 4 :(得分:0)

添加

circularImageView.layer.masksToBounds = true

你的逻辑。

查看文档:{​​{3}}

答案 5 :(得分:0)

circularImageView.layer.cornerRadius = circularImageView.frame.size.width / 2
circularImageView.layer.masksToBounds = true
circularImageView.clipsToBounds = true

请使用此代码

答案 6 :(得分:0)

检查控制台日志。在我的情况下,当我遇到类似的错误时,我在控制台中得到了类似的内容: -

[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 


(
    "<NSLayoutConstraint:0x60000009cd90 UIImageView:0x7fe661539400.height == 46   (active)>",
    "<NSLayoutConstraint:0x60000009ce30 V:|-(5)-[UIImageView:0x7fe661539400]   (active, names: '|':UITableViewCellContentView:0x7fe661531100 )>",
    "<NSLayoutConstraint:0x60000009ce80 V:[UIImageView:0x7fe661539400]-(5)-|   (active, names: '|':UITableViewCellContentView:0x7fe661531100 )>",
    "<NSLayoutConstraint:0x60000009d6f0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe661531100.height == 43.5   (active)>"
)

如果您注意到最后一个约束(UIView-Encapsulated-Layout-Height),您将看到系统自身采用不同的tableview单元格高度。所以最初你的imageView高度是你在约束中设置的(对我而言是46),因此imageView.bounds.height / 2变为23.但是当显示表时,系统给它一个不同的高度(43.5)。在最后一个约束中提到。

由于我的imageview对单元格也有顶部和底部约束(从5到顶部,从5到底部),它的大小调整为:43.5 - 5(顶部) - 5(底部)= 33.5

所以现在系统在尺寸为33.5的图像上应用了角半径23,这是第一次导致非圆形图像的原因。第二次以后,图像大小为33.5,我们得到imageView.bounds.height / 2 = 33.5 / 2 = 16.75。因此它从第二次起作用。

为了确保在给出图像高度约束以及顶部和底部约束时tableview不应用它自己的单元格高度,我们可以在代码中执行类似的操作。

    self.tblDemo.estimatedRowHeight = 70.0
    self.tblDemo.rowHeight = UITableViewAutomaticDimension

这应该可以解决您的问题,并且第一次为您提供圆形图像。

答案 7 :(得分:0)

let cellHeight: CGFloat = 100
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return cellHeight
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tbvCustom.dequeueReusableCell(withIdentifier: "CustomeCell", for: indexPath) as? CustomeCell else {return UITableViewCell()}
    cell.imvThumbnail.layer.cornerRadius = (cellHeight - cell.layoutMargins.top * 2)/2 }