0

I have set the corner radius to 10. But the image view does not show corner radius.

    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.image = UIImage(named: "sample_image")
        imageView.layer.cornerRadius = 20/2
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        return imageView

    }()

I have also Tried using imageView.layer.masksToBounds = true but it is also not working.

When I used background color on the image view as follows it shows the view with round corners perfectly.

    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.backgroundColor = UIColor.init(hexString: "#000000")
        imageView.layer.cornerRadius = 20/2
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        return imageView

    }()

I also tried changing the image, width and height of the image to check if the image is too large for the view. But it is also not working.

This is how i call the imageView.

       view.addSubview(imageView)

        self.imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

        self.imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: imageViewTopMargin).isActive = true

        self.imageView.widthAnchor.constraint(equalToConstant: imageViewWidth).isActive = true

        self.imageView.heightAnchor.constraint(equalToConstant: imageViewHeight).isActive = true
3
  • Did you check the view debugger ? Commented Apr 25, 2021 at 11:45
  • 1
    I have tested your code, and it is working perfectly fine! Check out the debug hierarchy, try different images, this must work fine. Commented Apr 25, 2021 at 12:07
  • 1
    Works fine for me too: screenshot
    – aheze
    Commented Apr 25, 2021 at 15:28

2 Answers 2

1

you need to change the contentMode of the UIImageView to center and clipToBound to true.

-1
imageView.layer.masksToBounds = true
1
  • 1
    Shouldn't clipsToBounds have the same effect?
    – aheze
    Commented Apr 25, 2021 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.