IB 渲染故事板时的可设计错误

IBDesignable errors when rendering storyboard

我尝试按照以下指南进行操作 1 and 2,但是当我尝试在情节提要中呈现自定义视图时出现错误。

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

这是我目前尝试过的方法:

@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
    }
}

并且我已经设置了自定义视图的文件所有者。

我创建了一个新的空项目来解决这个问题:

https://github.com/ivangodfather/CustomView

https://ufile.io/imr9p

问题似乎在捆绑包中。

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

有关获取错误日志的更多信息,您可以查看此答案:

请试试这个替换功能

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
    }