我有一个自定义 View (xib),其中包含UIButton,我通过以下操作将id设置为IBDesignable:

UserView.swift

import UIKit

@IBDesignable
class UserView: UIView {

    @IBOutlet var view: UIView!
    @IBOutlet weak var userButton: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        load()

    }

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

    fileprivate func load() {
        Bundle.main.loadNibNamed("UserView", owner: self, options: nil)
        addSubview(view)
        self.frame = bounds
    }

}

UserView.xib
  • 添加了UIButton并设置约束
  • 设置文件的所有者:UserView

  • Storyboard

    我在Bar Button Item中添加了一个UIView并分配了UserView类,但是没有呈现任何内容,并且出现了以下构建错误:



    我当前的环境: Xcode 9 ,Swift 4

    最佳答案

    我只是遇到了完全相同的问题,对我而言唯一有效的方法是使用主 bundle 包,而不必使用我想要的 Nib bundle 包。本质上变化:

    Bundle.main.loadNibNamed("UserView", owner: self, options: nil)
    

    到:
    let bundle = Bundle(for: UserView.self)
    bundle.loadNibNamed("UserView", owner: self, options: nil)
    

    这很奇怪,就我而言,在LLDB中在运行时比较两个Bundle时,它们是相同的(类名不同,但其余设置与OP相同)

    ios - IB Designables : Failed to render and update auto layout status-LMLPHP
    更新了

    为什么从主 bundle 包加载NIB不起作用?

    因为当InterfaceBuilder呈现时,它没有运行该应用程序。没有“主应用程序包”。

    关于ios - IB Designables : Failed to render and update auto layout status,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46723683/

    10-12 00:16
    查看更多