0

I'm trying to draw a circular image in a tableview cell. I have figured out how to draw the circle when the image is in a normal view: I place this code in viewDidLayoutSubviews () so as to apply my changes to the image only after autolayout has done its thing.

 profilePicture2.layer.cornerRadius = profilePicture2.frame.size.height/2
    profilePicture2.clipsToBounds = true

I got that tip from this answer: Why does adding an align with centre constraint to a UIImage throw out the radius of corners setting?

But how do I do it if the image is in a dynamic tableview cell? Is there a way to use viewDidLayoutSubviews() for a single cell, so I can set the corner radius of my image AFTER autolayout has done its thing?

Thanks in advance.

Community
  • 1
  • 1
thecloud_of_unknowing
  • 1,103
  • 14
  • 23

1 Answers1

2

You can create a custom class for profilePicture which extends UIView and override layoutSubviews.

override func layoutSubviews() {
    layer.cornerRadius = frame.size.height/2
}
Stijn van der Laan
  • 466
  • 1
  • 4
  • 10