UIButton custom class

0

I have the following custom class of a UIButton , but when I add it to a button in Xcode I can not make the changes be reflected. 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

    }

}
    
asked by Rogelio Sanchez 27.12.2018 в 04:47
source

1 answer

1

Assuming the problem is a EXC_BAD_INSTRUCTION , this is caused by a forced downcast from UIColor to CGColor . Try to change the

self.layer.backgroundColor = ColorUtils.hexStringToUIColor(hex: "#6B00FF") as! CGColor

for a

self.layer.backgroundColor = ColorUtils.hexStringToUIColor(hex: "#6B00FF").cgColor

as you did for borderColor .

(Or you can even delete the entire instruction, since you are also assigning the same color to backgroundColor of UIView .)

If the problem is that the rounded edges are not shown, then just put in your setup() :

layer.maskToBounds = true

(Or in the Attributes Inspector button, check the Clip to Bounds box in the Drawing section.)

    
answered by 27.12.2018 / 19:26
source