Anders Andersen
Anders Andersen

Reputation: 2517

IBDesignable can't set background on buttons

I'm trying to create a custom button in my IOS project. But when I'm creating the button with the my class the background doesn't work in the storyboard. But when I run the program the button works fine.

Here you see my class

@IBDesignable class GreenButtonTest : UIButton{

    override init(frame: CGRect) {
        super.init(frame: frame)
        style()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        style()
    }

    private func style() {
        self.backgroundColor = UIColor(CGColor: "#6eb18c".CGColor)
    }
}

Upvotes: 0

Views: 143

Answers (1)

Roel Koops
Roel Koops

Reputation: 880

You need to override prepareForInterfaceBuilder() and call style() from there too.

Try this:

@IBDesignable class GreenButtonTest : UIButton{

    override init(frame: CGRect) {
        super.init(frame: frame)
        style()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        style()
    }

    override func prepareForInterfaceBuilder() {
        style()
    }

    private func style() {
        self.backgroundColor = UIColor.greenColor()
    }

}

Upvotes: 1

Related Questions