2

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

aircraft
  • 25,146
  • 28
  • 91
  • 166
Godfather
  • 4,040
  • 6
  • 43
  • 70

2 Answers2

1

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

TomCobo
  • 2,886
  • 3
  • 25
  • 43
0

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
    }
karthikeyan
  • 3,821
  • 3
  • 22
  • 45