3

I'm trying this IBDesignable. But as always it don't work.

My controller code:

@IBDesignable
class MainMenuViewController: UIViewController {
    @IBOutlet weak var languagesButton: UIView!
    @IBOutlet weak var keyboardSettingsButton: UIView!
    @IBOutlet weak var aboutButton: UIView!
    @IBOutlet weak var helpButton: UIView!

    @IBInspectable var buttonsCornerRadius: CGFloat = 5 {
        didSet {
            languagesButton.layer.cornerRadius = buttonsCornerRadius
            keyboardSettingsButton.layer.cornerRadius = buttonsCornerRadius
            aboutButton.layer.cornerRadius = buttonsCornerRadius
            helpButton.layer.cornerRadius = buttonsCornerRadius
        }
    }

    override func prepareForInterfaceBuilder() {
        languagesButton.layer.cornerRadius = buttonsCornerRadius
        keyboardSettingsButton.layer.cornerRadius = buttonsCornerRadius
        aboutButton.layer.cornerRadius = buttonsCornerRadius
        helpButton.layer.cornerRadius = buttonsCornerRadius
    }
}

I have this property in my interface builder:

enter image description here

But it never update my storyboard. And when I run my application it just crash with no message.

enter image description here

enter image description here

Vasyl Khmil
  • 2,548
  • 1
  • 20
  • 36
  • 1
    did you get a "unexpectedly found nil while unwrapping an Optional value" ? Because I think that all your IBOutlet properties are nil – Ali Abbas Feb 17 '15 at 10:54

1 Answers1

3

As I said in my comment, all your IBOutlets are nil on this stage. You have an "unexpectedly found nil while unwrapping an Optional value"

What I can suggest is to make a custom class of UIView and then inside this class, you can add a property "cornerRadius" as @IBInspectable like this :

import UIKit

@IBDesignable

class MyCustomView:UIView {
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
        }
    }
}

Using this approach and every time you change the value in IB, your view will be updated.

Ali Abbas
  • 4,247
  • 1
  • 22
  • 40