Search code examples
swiftuiimageview

Image in circle not working in iPhone 5 (works in iPhone 7)


In my iPhone 7 simulator, my image is an exact circle. For some reason my image in iPhone 5 becomes a little squarish. This is the code that I have to make the image a circle:

    private func setImage(){
    self.profileImage.layer.borderWidth = 0.0;
    self.profileImage.layer.cornerRadius = self.profileImage.frame.size.height/2;
    self.profileImage.clipsToBounds = true
    }

There is a 1:1 aspect ratio constraint on the image. Also it works on iPhone 7 perfectly

And I add an image to show the problem in the result in iPhone 5.

enter image description here


Solution

  • I found the problem. Since in storyboard I am working in iphone 7, I have to add this command in my view did load, so the frame updates the constraints for iphone 5 before making the change:

    self.view.layoutIfNeeded()
    

    I hope this helps people!