When I write my own UIButton
-extended class and make it @IBDesignable
, I receive two errors in Interface Builder, namely:
In Xcode 7.3, none of the above solutions worked for me, but the accepted answer to this question did: Failed to render instance of IB Designables
- Clear Xcode derived data for the project. They are in ~/Library/Developer/Xcode/DerivedData
- Clean your current build by pressing ⌘⇧K
- Build your project
- In storyboard go to Editor menu and do Refresh All Views; wait for build to be completed and errors should be gone
I did not need to do the 4th step to solve the problem (and get my PaintCode-drawRect'd UIView to paint in the storyboard), included here just in case.
Credit to @Mojtaba and @WeZZard
I wasted a full day on this and finally I got my problem solved
I selected Build for target as 8.0 and it fixes everything for me.
For me, it was not using #if !TARGET_INTERFACE_BUILDER
that got me. Basically I had some code that would result in accessing a path in my Bundle...
Bundle.main.path(forResource: "Foo", ofType: "plist")
The problem is that when running in IB (and not in your app), Bundle.main
isn't your app...
(lldb) po Bundle.main
NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays> (loaded)
So the answer to this is simply to review your @IBDesignable UIView
code carefully and use #if !TARGET_INTERFACE_BUILDER
for anything (in my case calls to CoreLocation
for example) that doesn't make sense when running at design time.
This is what you see when using the Editor -> Debug Selected View
while selecting your @IBDesignable UIView
in your storyboard:
In my case, as you can see initXXXX
was doing an assert
, which was crashing at design time, because it was looking for a value in file in my Bundle — which is Xcode, at design time.
XCode 10, Swift 4.2
I found an answer here
Solution was simple - changing the way the bundle was resolved in the required init?(coder aDecoder: NSCoder)
method of my custom view class.
Bundle.main.loadNibNamed(String(describing: TestView.self), owner: self, options: nil)
needed to be changed to
let bundle = Bundle(for: TestView.self)
bundle.loadNibNamed(String(describing: TestView.self), owner: self, options: nil)