Make images round in swift?
It displays the shape of the diamond because you set the cornerRadius before resizing the view.
This will result in a diamond shape:
var myView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
myView.backgroundColor = UIColor.redColor()
view.addSubview(myView)
myView.layer.cornerRadius = myView.frame.size.width / 2
// setting frame doesn't change corner radius from the former large value
myView.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
You can set this just before the view is rendered by doing this in viewWillAppear:
source to share
Maybe you are missing something, below code works for me:
profilePicture.layer.borderWidth=1.0
profilePicture.layer.masksToBounds = false
profilePicture.layer.borderColor = UIColor.whiteColor().CGColor
profilePicture.layer.cornerRadius = profilePicture.frame.size.height/2
profilePicture.clipsToBounds = true
Note. To get a perfect circle, the frame of the image must be square.
source to share
To get any UIImageview
in a round shape, you need to first set its Frame to a square shape. For example, if you UIImageView
have a Frame, (20,20,100,100)
you will see that the height and width are the same. Then you need to set the cornerradius
angle of this to UIImageview
half the size of its height or width .
I did something like this in Xamarin.iOS: -
imageView.Layer.CornerRadius = imageView.Frame.Size.Width / 2;
imageView.ClipsToBounds = true;
source to share