Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface builder agent crashes when accessing @IBInspectable UIImage

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?

like image 400
cybergen Avatar asked Jan 06 '23 19:01

cybergen


1 Answers

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) 
like image 140
A. Maly Avatar answered Jan 21 '23 02:01

A. Maly