The round corners of the UIImageView work. But I don't get the circle
I am trying to apply round corners to a UIImageView. Although the corners are round, there are 4 more edges that just won't disappear regardless of the size of the radius. Removing the border doesn't help either.
Could this have something to do with the limitations of auto layout? What am I doing wrong?
Here is the code I am applying:
self.imageViewProfilePicture.layer.cornerRadius = self.imageViewProfilePicture.frame.size.width / 2.0
self.imageViewProfilePicture.layer.borderWidth = 2.0
self.imageViewProfilePicture.layer.borderColor = UIColor.whiteColor().CGColor
self.imageViewProfilePicture.layer.masksToBounds = true
self.imageViewProfilePicture.clipsToBounds = true
+3
source to share
2 answers
Since you are using constraints to define the width and height of the image, the final frame will be determined after the subview layout. In your case, just move your code to viewDidLayoutSubviews
:
- (void)viewDidLayoutSubviews
{
super.viewDidLayoutSubviews()
self.imageViewProfilePicture.layer.cornerRadius = self.imageViewProfilePicture.frame.size.width / 2.0
self.imageViewProfilePicture.layer.borderWidth = 2.0
self.imageViewProfilePicture.layer.borderColor = UIColor.whiteColor().CGColor
self.imageViewProfilePicture.layer.masksToBounds = true
self.imageViewProfilePicture.clipsToBounds = true
}
+8
source to share