My application is mostly round- and borderbased.
I use the layer property of UIView
to give a corner radius and a border.
But I am facing a problem that the corners are not clear.
I am getting the following results:
UIButton
UIImageView
You can observe a thin border line around the white or grey border.
This is my code:
button.layer.borderWidth = 2.0;
button.layer.borderColor = [[UIColor whiteColor] CGColor];
button.layer.cornerRadius = 4;
button.clipsToBounds = YES;
I have searched to solve this but I'm not getting success.
I have tried button.layer.masksToBounds = YES
, but with no effect.
Am I missing anything? Or are there any other methods which can give me better results compared to CALayer
?
The answer by CRDave works great but has one flaw: When called multiple times like on size change, it keeps adding layers. Instead previous layers should be updated.
See below for an updated ObjC version. For swift please adapt accordingly.
delete
Here is a Swift 5 version of @CRDave's answer as an extension of UIView:
de.'s answer worked much better for me than CRDave's answer.
I had to translate it from Swift, so I thought I'd go ahead and post the translation:
I'm calling the method from
layoutSubviews()
I tried many solution and end by using
UIBezierPath
.I create category of
UIView
and add method to make round rect and border.This is method of that category:
I get idea of using
UIBezierPath
from this amazing article: Thinking like a Bézier pathI get most of code from this two link:
Note: This is category method so self represent view on which this method is called. Like UIButton, UIImageView etc.
This is Kamil Nomtek.com's answer updated to Swift 3+ and with some refinements (mostly semantics/naming and using a class protocol).