Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular ImageView comes out as diamond

Tags:

ios

swift

I'm trying to make my imageView a circle, but it keeps coming out as a diamond. I thought this code would work but it's not:

profilePic.layer.cornerRadius = profilePic.frame.width / 2
    profilePic.clipsToBounds = true
like image 643
cb428 Avatar asked Oct 30 '22 12:10

cb428


2 Answers

Try your code in

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    profilePic.layer.cornerRadius = profilePic.frame.width / 2
    profilePic.clipsToBounds = true
}
like image 125
Jaydeep Patel Avatar answered Nov 13 '22 06:11

Jaydeep Patel


You might not get perfect circle in every device, try changing 2.0 to different near by values for different size devices.

-(void)viewDidLoad {
    self.profilePic.layer.cornerRadius = self.profilePic.frame.size.width / 2.0f;
    self.profilePic.clipsToBounds = YES;
}
like image 26
Arpit Dongre Avatar answered Nov 13 '22 06:11

Arpit Dongre