I have the following custom class of a UIButton
, but adding it to a button in Xcode fails to make the changes reflect. Does anyone know what the problem is?
import UIKit
class BotonBordesRedondeados: UIButton {
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
/*override func draw(_ rect: CGRect) {
// Drawing code
setup()
}*/
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup () {
self.layer.backgroundColor = ColorUtils.hexStringToUIColor(hex: "#6B00FF") as! CGColor
self.backgroundColor = ColorUtils.hexStringToUIColor(hex: "#6B00FF")
layer.borderColor = ColorUtils.hexStringToUIColor(hex: "#6B00FF").cgColor
layer.cornerRadius = 20
layer.borderWidth = 2.0
}
}
Assuming the problem is a
EXC_BAD_INSTRUCTION
, it is caused by a forced downcast fromUIColor
aCGColor
. Try to change thefor
How did you do it for him
borderColor
?(Or you can even delete the statement entirely, since you're also assigning the same color to
backgroundColor
delUIView
.)If the problem is that the rounded edges are not showing, then just put inside your
setup()
:(Or in the button's Attributes Inspector check the box in the Drawing
Clip to Bounds
section .)