Martin Velchevski Martin Velchevski - 2 months ago 40
Swift Question

@IBDesignable fails to render class instance and crashes agent with Swift 3

I have an xib which loads perfectly well after building the project. At build-time though I get the following error:

Main.storyboard: error: IB Designables: Failed to render instance of PalitraPaletteSelector: The agent crashed


I've read a lot of answers, even cleaned the DerivedData folder to no avail. Both initializers are implemented so I don't see what I'm missing here.

Here's the code I have:

@IBDesignable
class PalitraPaletteSelector: NSStackView {

@IBOutlet var view: NSStackView!

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

override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
loadViewFromNib()
}

func loadViewFromNib() {
Bundle.main.loadNibNamed("PalitraPaletteSelector", owner: self, topLevelObjects: nil)
view.frame = self.bounds
self.addSubview(view)
}

}

Answer

You cannot use Bundle.main in a designable view. How about using Bundle(for: type(of: self)) instead.

Comments