Nguyen van Duc
Nguyen van Duc

Reputation: 91

cornerRadius of UIImageView not working

I had set cornerRadius like below:

@IBDesignable class CornerImageView: UIImageView {
@IBInspectable var cornerRadiusValue: CGFloat = 10.0 {
    didSet {
        setUpView()
    }
}
override func awakeFromNib() {
    super.awakeFromNib()
    setUpView()
}
override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    setUpView()
}

func setUpView() {
    self.layer.cornerRadius = self.cornerRadiusValue == -1 ? self.bounds.size.height/2 : self.cornerRadiusValue
    self.clipsToBounds = true
}
}

On storyboard, it worked correctly. But failed when run on device. Please help!!!

wrong cornerRadius

on Storyboard

Upvotes: 0

Views: 1470

Answers (4)

OMEESH SHARMA
OMEESH SHARMA

Reputation: 141

Actual frame of the Image will be set in viewDidAppear or LayouSubviews. Just add the following Code.

override func layoutSubviews() {
    super.layoutSubviews()
    setUpView()
}

Upvotes: 1

SThakur
SThakur

Reputation: 282

Try to set corner radius half of the height of the UIImageView in viewDidLayoutSubviews method.

Upvotes: 0

Aravind Vijayan
Aravind Vijayan

Reputation: 155

Try This

func setUpView() {
        self.layer.cornerRadius = self.frame.width/2;
        self.layer.masksToBounds = true;
    }

Upvotes: 2

Sanjukta
Sanjukta

Reputation: 1055

Please try this :

Set the image view height and width same or set the accept ratio to 1:1.

And put this code

imageview.layer.cornerRadius = imageview.frame.size.width / 2
imageview.clipsToBounds = true

It may helps you.Thank you

Upvotes: 1

Related Questions