I'm implementing a custom view by subclassing UIView
and using @IBInspectable
for some of my variables. Two of them are UIImage. If I try to access one of them in the code, the interface builder crashes with the following message:
file:///PathToMyProject/MyProject/Pod/Classes/UI/View/MyView.xib: error: IB Designables: Failed to update auto layout status: The agent crashed
and
file:///PathToMyProject/MyProject/Pod/Classes/UI/View/MyView.xib: error: IB Designables: Failed to render instance of MyView: The agent crashed
Everything runs fine on simulator and device.
Here's how you can reproduce it, assumed image_one.png
is in the assets:
import UIKit
@IBDesignable
class MyView: UIView {
@IBInspectable
var anImage: UIImage = UIImage(named: "image_one")!
}
In fact the interface builder agent crashes at initializing this variable. If you write var anImage: UIImage! = UIImage(named: "image_one")
, then the agent crashes when accessing this variable (as stated above).
Any ideas?
You do not have to set you image in inspector in IB heres a solution purely in code.
let bundle = Bundle(for: self.classForCoder)
image = UIImage(named: "your_image.png", in: bundle, compatibleWith: self.traitCollection)!
self.setImage(image, for: .normal)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With