Formatting Objects

Formatting Buttons and Labels

It is possible to give buttons and labels a nice rounded corner. The first step is to set up each item as an IBOutlet (even the buttons). For example.

@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!

The next step is to add the code in a function such as viewDidLoad

button1.layer.cornerRadius = 5.0
button2.layer.cornerRadius = 5.0
button3.layer.cornerRadius = 5.0
button4.layer.cornerRadius = 5.0

Note the cornerRadius is a property of the layer of the button not the button it's self.

Finally, make sure that Clip to Bounds is checked for each object in the Attribute Inspector.

Clip to Bounds image

This gives a nice round corner under program control.

Rounded corner


Index


Comments, Corrections, Suggestions: David Bourne (david@boomer.org)
My iOS and tvOS Apps and iBooks