Circular ImageView comes out like a diamond

I try to make my imageView circle, but it continues to appear like a diamond. I thought this code would work, but it is not:

profilePic.layer.cornerRadius = profilePic.frame.width / 2 profilePic.clipsToBounds = true 
+5
source share
5 answers

Try your code in

 override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() profilePic.layer.cornerRadius = profilePic.frame.width / 2 profilePic.clipsToBounds = true } 
+1
source

You may not get the perfect circle in every device, try changing 2.0 to different values ​​next to different size devices.

 -(void)viewDidLoad { self.profilePic.layer.cornerRadius = self.profilePic.frame.size.width / 2.0f; self.profilePic.clipsToBounds = YES; } 
+1
source

Manually implement init(coder aDecoder: NSCoder!) adding the code below destinationViewController .

initialize ImageView as under

 init(coder aDecoder: NSCoder!) { super.init(coder: aDecoder) profiePic = [[UIImageView alloc]init]; profilePic setFrame : CGRectMake ("Set Your Control Frame here"); profilePic.layer.cornerRadius = profilePic.frame.width / 2 profilePic.clipsToBounds = true } 

You will get your result.

0
source

It works for me

 profilePicImageView.layer.cornerRadius = profilePicImageView.frame.size.height / 2 profilePicImageView.layer.masksToBounds = false profilePicImageView.clipsToBounds = true profilePicImageView.layer.borderWidth = 0 
0
source

Use the same code in the viewDidLayoutSubviews or viewDidAppear . In fact, according to the life cycle of the view, the methods are executed as follows: init , loadView , viewDidLoad , viewWillAppear , viewWillLayoutSubViews , then the autodetection restrictions are applied (the number of autodetection restrictions you have, this layout method is called), then viewDidLayoutSubviews and finally viewDidAppear .

From your question, it looks like you put your code in any of the methods before the autodetection restrictions are applied. This is why you are not getting the right viewing width, and it turns into a diamond instead of a circle.

0
source

Source: https://habr.com/ru/post/1239327/


All Articles