【问题标题】:IBDesignable errors when rendering storyboard渲染故事板时出现 IBDesignable 错误
【发布时间】:2018-01-04 10:39:24
【问题描述】:

我尝试遵循12 中的一些指南,但是当我尝试在情节提要中呈现自定义视图时出现错误。

无法渲染和更新视图控制器的自动布局状态。代理抛出异常。

这是我迄今为止尝试过的:

@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

【问题讨论】:

  • 请将该 ufile 链接后面的任何内容上传到此站点。将代码或图像放在这里。
  • 很遗憾我不能在这里上传我的项目

标签: ios swift ibdesignable


【解决方案1】:

问题似乎在捆绑包中。

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

有关获取错误日志的更多信息,您可以查看此答案:https://stackoverflow.com/a/42678873/2000162

【讨论】:

    【解决方案2】:

    请试试这个替换功能

    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
        }
    

    【讨论】:

      猜你喜欢
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      相关资源
      最近更新 更多