I\'m building an app using swift in the latest version of Xcode 6, and would like to know how I can modify my button so that it can have a rounded border that I could adjust mys
I have created a simple UIButton sublcass that uses the tintColor
for its text and border colours and when highlighted changes its background to the tintColor
.
class BorderedButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
layer.borderWidth = 1.0
layer.borderColor = tintColor.CGColor
layer.cornerRadius = 5.0
clipsToBounds = true
contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
setTitleColor(tintColor, forState: .Normal)
setTitleColor(UIColor.whiteColor(), forState: .Highlighted)
setBackgroundImage(UIImage(color: tintColor), forState: .Highlighted)
}
}
This makes use of a UIImage extension that creates an image from a colour, I found that code here: https://stackoverflow.com/a/33675160
It works best when set to type Custom in interface builder as the default System type slightly modifies the colours when the button is highlighted.