iosswiftswift2ios9xcode7-beta6

How to perform changes live on UIView in Storyboard without setting any @IBinspectable property?


In Storyboard I have UITextField, I also created custom class:

class WLTextField: UITextField {

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

        layer.cornerRadius = 2
        layer.masksToBounds = true
        layer.borderColor = UIColor.grayColor().CGColor
        layer.borderWidth = 1
    }
}

Then attached this class to my UITextField:

enter image description here

But, there is no result for this in Storyboard. How can I achieve this? I need to get such effect like it is with @IBDesignables. But since I know what I need to set up for every text field, I do not need set a value and then update this within didSet.

Do I need to set at least one value to make changes live in Storyboard on some UIView?


Solution

  • In order for a component to render in the Interface Builder you must flag the class as @IBDesignable and you must implement the initialise init(frame: CGRect) -

    @IBDesignable class WLTextField: UITextField {
    
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setupLayer()
        }
    
        required override init(frame: CGRect) {
            super.init(frame: frame)
            setupLayer()
        }
    
        func setupLayer () {
            layer.cornerRadius = 2
            layer.masksToBounds = true
            layer.borderColor = UIColor.grayColor().CGColor
            layer.borderWidth = 1
        }
    }