How To Set Corner Radius for UIButtons Using Swift

175 0

I recently answered this question on Quora. So just how do you set the corner radius on an iOS button? Turns out, it is very straight forward with just a couple of lines of Swift code.

First, create an outlet for the button so we can set the properties when the view loads.

@IBOutlet weak var myButton: UIButton!

Next, in the viewDidLoad() assign the value and set the clipsToBounds to true.

myButton.layer.cornerRadius = 100
myButton.clipsToBounds = true

With that, we now have a radius of 100 on the corners, so if you have a button that is 200 by 200 it will turn from a square to a circle. Note that I changed the button background color to make it easy to see.

Example of an iOS button with rounded corners.
Example of an iOS button with rounded corners.