0

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)
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Anders Andersen
  • 2,377
  • 2
  • 17
  • 25

1 Answers1

1

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()
    }

}
Roel Koops
  • 840
  • 2
  • 16
  • 28