Search code examples
iosswiftautolayout

Image View is not a perfect circle


I have a UITableViewCell which contains an UIImageView.

The image view should be a circle, so I made the cornerRadius equal to half of its height. The problem is that if the image is not big enough (the width/height constraints of the image view, not the image itself), it is not rounded correctly. If I increase the constraints, everything will work fine.

The image is a square and the image view also has a 1:1 ratio constraint. Any idea what the problem is?

I call this in awakeFromNib:

private func setUpCellDesign() {
    profileImageView.layer.cornerRadius = profileImageView.frame.height / 2
    profileImageView.clipsToBounds = true
    mainView.layer.cornerRadius = 15

}

I have noticed that it doesn't work as wanted even if I add other elements in the cell and try to round the corners in the same way (for example a button with rounded corners).


Solution

  • It seems that you have an issue with the layout-subviews of the cell - you need a few more cycles of the auto-layout mechanism

    You can solve it by overriding layoutSubViews and put the snip code you wrote in there.

    You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.