Seems like I am using @IBDesignbale
wrong, but why?
I want to use a combination of IBDesignable
and prepareForInterfaceBuilder
to inject some data into my MyTableViewCell
so it can display some dummy data(in this sample project, just set the dummyView.backgroundColor = .red
) in my storyboard.
My current setup:
1, I guess the usual setup for a tableview using storyboard. TableView inside a view and dragged a UITableViewCell
into the tableview.
2, marked my MyTableViewCell
@IBDesignable
3, override prepareForInterfaceBuilder
inside the MyTableViewCell
setting the dummyView
backgroundColor
error:
Main.storyboard: error: IB Designables: Failed to render and update auto layout status for ViewController (BYZ-38-t0r): The agent crashed
This is happening because your dummyView
is an IBOutlet
and is implicitly unwrapped. prepareForInterfaceBuilder
will be called before dummyView
is initialized. You can prevent a crash by changing your code to dummyView?.backgroundColor = .red
but then nothing will be rendered because dummyView == nil
.
It doesn't make a ton of sense to mix IBDesignable
with IBOutlet
. In general, IBDesignable
is meant to make run time layout and drawing visible at design time. But IBOutlets
are necessarily already visible at design time. This might however be desirable in a xib. For a discussion of that see here and here.