How to set corner radius of imageView?
Solution 1
Layer draws out of clip region, you need to set it to mask to bounds:
self.mainImageView.layer.masksToBounds = true
From the docs:
By default, the corner radius does not apply to the image in the layer’s contents property; it applies only to the background color and border of the layer. However, setting the masksToBounds property to true causes the content to be clipped to the rounded corners
Solution 2
There is one tiny difference in Swift 3.0 and Xcode8
Whenever you want to apply corner radius to UIView, make sure you call
yourUIView.layoutIfNeeded()
before calling cornerRadius
.
Otherwise, it will return the default value for UIView's height and width (1000.0) which will probably make your View disappear.
Always make sure that all effects that changes the size of UIView (Interface builder constraints etc) are applied before setting any layer properties.
Example of UIView class implementation
class BadgeView: UIView {
override func awakeFromNib() {
self.layoutIfNeeded()
layer.cornerRadius = self.frame.height / 2.0
layer.masksToBounds = true
}
}
Solution 3
You can define border radius of any view providing an "User defined Runtime Attributes", providing key path "layer.cornerRadius" of type string and then the value of radius you need ;) See attached images below:
Solution 4
try this
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0
self.mainImageView.clipsToBounds = true
Solution 5
Marked with @IBInspectable
in swift (or IBInspectable in Objective-C), they are easily editable in Interface Builder’s attributes inspector panel.
You can directly set borderWidth,cornerRadius,borderColor in attributes inspector
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get{
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = borderColor?.cgColor
}
}
}
Comments
-
theDC over 4 years
In Objective-C such line
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0f;
does its job, I tried it in Swift using analogy
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0
and it doesn't change anything, the corners are the same as before. Moreover, Xcode does not show any syntax errors. Does Swift support any other way to reach this goal? I checked some other threads here and usually it's getting done in Swift in the way showed above.
-
theDC over 9 yearsI missed that line, now works perfectly fine, thank you @DarthMike !
-
erics over 7 yearsmasksToBounds was also needed for me to get it to work after the update to Swift 3.0
-
Joe Sene almost 7 yearsBorder Color is not working. It returns black always.
-
iman kazemayni over 6 yearsthis answer is correct for roundcorner after get image from url and set to imageview
-
Vincent Gigandet about 6 yearsThis is a clean & simple solution
-
fredericdnd over 5 yearsCalling from the main thread helped me because I rounded
containerView
- which was a subview of my tableView cells - in thewillDisplay
tableView delegate method. Without calling it from the main thread, the frame was wrong -
Phuc Dang almost 5 years@JoeSene layer.borderColor = borderColor?.cgColor is wrong, just update it like this: layer.borderColor = newValue?.cgColor
-
rolandforbes over 3 yearsmy guuuyyyy nice!