Godfather
Godfather

Reputation: 4330

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

Upvotes: 2

Views: 684

Answers (2)

karthikeyan
karthikeyan

Reputation: 3888

Kindly try this replace function

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

Upvotes: 0

TomCobo
TomCobo

Reputation: 2916

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

Upvotes: 1

Related Questions