Search code examples
iosswift

How to set cornerRadius on a imageview inside a table view cell


I created a customized table view cell and added a UIImageView on that cell. I want to make the image view to be circular, see below code. There is a problem that when the table is showing the first time, the circularImageView.bounds value is not correct which makes the image not circular. After refresh the table view the image looks correct. How to handle the first time loading of the table view?

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
   }

I have tried to set this value on cellForRowAtIndexpath. But the circularImageView.bounds.height is not constrained when the first time showing this cell


Solution

  • set corner Radius in awakeFromNib method.

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