My goal is to create a button with fully rounded corners (half a circle at each side). It should look something like this:
In the user defined runtime attributes I’ve set layer.cornerRadius as KeyPath and 50 as Value (also tried 49). Also, I’ve set the the “Clip Subviews” option to yes (like in this answer: UIlabel layer.cornerRadius not working in iOS 7.1)
However, the compiled result looks like this on my iPhone 5c (iOS 9.2.1):
(The images are made in photoshop but that’s exactly how it looks like)
The corners are rounded but there is clearly an anchorpoint missing. Any suggestion on how to get the desired result?
3
Answers
Try this if you want to have a dynamic approach rather than using fixed values.
The
cornerRadius
value does not update automatically depending on button height, so, when specifying it from storyboard/xib, you have to make sure the value is exactly the half of the button height.Typically this is achieved by setting the button height to a constant (using a height constraint) and setting
cornerRadius
to half of that constant (e.g. for height100
you wantcornerRadius
with value50
).If you want an autoupdating solution, the easiest solution is to subclass
UIButton
and overridelayoutSubviews
:Then in xib/storyboard you can change the class of your button to your custom class.
For me the below worked better than anything else
Create a class for your rounded button and subclass UIButton.