Using swift, I'd like like to create a button with rounded corners. To make this re-usable, my preference is to subclass UIButton, and have come up with the following:
import Foundation
import UIKit
class LoginButton: UIButton {
let corner_radius : CGFloat = 4.0
override func drawRect(rect: CGRect) {
super.drawRect(rect)
self.layer.cornerRadius = corner_radius
}
}
Unfortunately, this doesn't seem to work as I had hoped, even though it compiles fine. Perhaps I'm missing something - I'm very new to this!
Any ideas?
To get round corners button using Interface Builder add a Key Path = "layer. cornerRadius" with Type = "Number" and Value = "10" (or other value as required) in the " User Defined RunTime Attribute " of the Identity Inspector of the button.
You need to use clipsToBounds
to ensure that the containing view isn't drawn over the corner radius:
self.clipsToBounds = true
You need also to turn on the masksToBounds
on the layer:
self.layer.masksToBounds = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With