2

I have an IBDesignable subclass of UIButton, but my changes don't appear on the storyboard where I use it. I have tried refreshing all views on my storyboard, or re-linking the button with the class, but to no avail.

The class:

@IBDesignable class MyButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

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

    func commonInit() {
        layer.cornerRadius = 3
        layer.masksToBounds = true
        backgroundColor = Colors.E9511C
        setTitleColor(.white, for: .normal)
        titleLabel?.font = Fonts.openSansBold(14)
    }
}
vrwim
  • 13,020
  • 13
  • 63
  • 118

3 Answers3

0

@IBDesignable class MyButton: UIButton {

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}
@IBInspectable var borderColor: UIColor = UIColor.white {
    didSet {
        layer.borderColor = borderColor.cgColor
    }
}
@IBInspectable var cornerRadius: Int = 1 {
    didSet {
        layer.cornerRadius = CGFloat(cornerRadius)
    }
}

func commonInit() {
    layer.cornerRadius = CGFloat(cornerRadius)
    layer.masksToBounds = true
    layer.borderColor = borderColor.cgColor
    //layer.bobackgroundColor = backgroundColor.cgColor // Colors.E9511C
    setTitleColor(.white, for: .normal)
    titleLabel?.font = UIFont.systemFont(ofSize: 14) // Fonts.openSansBold(14)
}

override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    commonInit()
}

}

MONIKA
  • 1
  • 1
-1

Add this to your MyButton class:

override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    commonInit()
}

You may also be running into an issue with your Colors and / or Fonts ... this full class works fine for me:

@IBDesignable class MyButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

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

    func commonInit() {
        layer.cornerRadius = 3
        layer.masksToBounds = true
        backgroundColor = UIColor.brown // Colors.E9511C
        setTitleColor(.white, for: .normal)
        titleLabel?.font = UIFont.systemFont(ofSize: 14) // Fonts.openSansBold(14)
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        commonInit()
    }
}

Result in Storyboard / Interface Builder:

enter image description here

DonMag
  • 69,424
  • 5
  • 50
  • 86
-2

Go to storyboard -> identity inspector ( there you have to set custom class name as MyButton) clean the project then build Hope it will work

MONIKA
  • 1
  • 1
  • It works when I build, it just doesn't show up on the storyboard – vrwim Jul 10 '18 at 13:15
  • use @IBInspectable ,then check the changes in mainstoryboard(attribute inspector). check the code above . it is working . – MONIKA Jul 12 '18 at 10:09