let radius = self.bounds.width / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
I applied the above code But it creates a strange shape.
I do not know why and how to fix it.;;
let radius = self.bounds.width / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
I applied the above code But it creates a strange shape.
I do not know why and how to fix it.;;
if width and height are equal this code works fine, change 2.5 to 2
let radius = self.bounds.width / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
To make UIImageView
circle you need to make sure you UIImageView
's Height and Width should me same(square).
Use divide by 2 instead of 2.5:
let radius = self.bounds.width / 2
imgView.layer.borderWidth = 1
imgView.layer.masksToBounds = false
imgView.layer.borderColor = UIcolor.black.cgColor
imgView.layer.cornerRadius = self.frame.height/2
imgView.clipsToBounds = true
self.view.layoutIfNeeded()
try this code or may be your code is perfect just put self.view.layoutIfNeeded()
in your code
If wanna make perfect round view must be square.
And if your view have aspect ratio then apply corner radius after all constraints loaded for view.
For that you can apply radius view into viewDidAppear
method.
Or viewDidLayoutSubviews
method.
or apply radius using delay.
Update
func delay(_ seconds: Double, completion: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
completion()
}
}
Make sure that the image height and width are same
override func viewDidLayoutSubviews() {
let radius = yourImageOutlet.bounds.width / 2
yourImageOutlet.layer.cornerRadius = radius
yourImageOutlet.layer.masksToBounds = true
}