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.
The easiest way is to create an UIImageView subclass (I have tried it and it's working perfectly on iPhone 7 and XCode 8):
and then you can also set a border:
in swift 3 'CGRectGetWidth' has been replaced by property 'CGRect.width'
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 callingcornerRadius
.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
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:
I created an
UIView
extension which allows to round specific corners :Layer draws out of clip region, you need to set it to mask to bounds:
From the docs: