Reputation: 91
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!!!
Upvotes: 0
Views: 1470
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
Reputation: 282
Try to set corner radius half of the height of the UIImageView
in viewDidLayoutSubviews
method.
Upvotes: 0
Reputation: 155
Try This
func setUpView() {
self.layer.cornerRadius = self.frame.width/2;
self.layer.masksToBounds = true;
}
Upvotes: 2
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