Search code examples
iosswiftibdesignable

IBDesignable errors when rendering storyboard


I tried to follow some guides as follows 1 and 2, but when I try to render a custom view in a storyboard I get an error.

Failed to render and update auto layout status for View Controller. The agent threw an exception.

This is what I have tried so far:

@IBDesignable final class CustomView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

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

    private func setup() {
        let customViewNib = loadFromNib()
        customViewNib.frame = bounds
        customViewNib.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        addSubview(customViewNib)
    }

    func loadFromNib() -> UIView {
        let nib = UINib(nibName: "CustomView", bundle: Bundle.main)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        return view
    }
}

And i have set the File Owner of my custom view.

I have create a new empty project faicing this issue:

https://github.com/ivangodfather/CustomView

or

https://ufile.io/imr9p

enter image description here


Solution

  • The issue it seems to be in the bundle.

    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
    

    For more information to get the logs of the error you can see this answer: https://stackoverflow.com/a/42678873/2000162