Linker Agent Agent Fails When Accessing @IBInspectable UIImage

I implement a custom view by subclassing UIViewand using @IBInspectablefor some of my variables. Two of them are UIImage. If I try to access one of them in the code, the interface constructor will exit 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 works fine on the simulator and device. Here, as you can reproduce it, it is assumed that it image_one.pngis in assets:

import UIKit

@IBDesignable
class MyView: UIView {

    @IBInspectable
    var anImage: UIImage = UIImage(named: "image_one")!

}

In fact, the interface initialization agent is reset when this variable is initialized. If you write var anImage: UIImage! = UIImage(named: "image_one"), then the agent crashes when accessing this variable (as indicated above).

Any ideas?

+4
3

, Interface Builder, , IB . , IB.

:

import UIKit

@IBDesignable
class MyView: UIView {

    @IBInspectable
    var anImage: UIImage? = UIImage(named: "image_one")

}
+2

IB, .

let bundle = Bundle(for: self.classForCoder)
image = UIImage(named: "your_image.png", in: bundle, compatibleWith: self.traitCollection)!
self.setImage(image, for: .normal) 
+1

ibinspectable UIImage. , . , UIImage:named:.

:

@IBInspectable var _iconImage: UIImage? = #imageLiteral(resourceName: "ic_arrow_dropdown") {
    didSet { iconImageView.image = _iconImage }
}

_

:

@IBInspectable var _iconImage: UIImage? = UIImage(named: "ic_arrow_dropdown") {
    didSet { iconImageView.image = _iconImage }
}

_

* I know that OP did not use image literal. Just in case someone needs it.

+1
source

Source: https://habr.com/ru/post/1625283/


All Articles