In Objective-C such line
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0f;
does its job, I tried it in
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
}
}