0
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.

enter image description here

I do not know why and how to fix it.;;

5 Answers5

3

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
Shezad
  • 756
  • 7
  • 14
0

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
iVarun
  • 6,496
  • 2
  • 26
  • 34
0
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

Mahesh Dangar
  • 806
  • 5
  • 11
0

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()
    }
}
Pratik Sodha
  • 3,679
  • 2
  • 19
  • 38
-1

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
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15