访问@IBInspectable UIImage时,接口构建器代理崩溃 [英] Interface builder agent crashes when accessing @IBInspectable UIImage
问题描述
我正在通过继承 UIView
并使用 @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
和
file:///PathToMyProject/MyProject/Pod/Classes/UI/View/MyView.xib: error: IB Designables: Failed to render instance of MyView: The agent crashed
一切都在模拟器和设备上运行良好。
以下是如何重现它,假设 image_one.png
在资产中:
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")!
}
事实上,界面构建器代理在初始化此变量时崩溃。如果你写 var anImage:UIImage! = UIImage(名为: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).
任何想法?
推荐答案
问题是Interface Builder中的图像路径与应用程序中的路径不同,因此IB正在取消引用nil图像。您可以通过使用选项,然后在IB中的属性检查器中设置图像来解决此问题。
The problem is that paths to images are different in Interface Builder than they are in your app, so IB is dereferencing a nil image. You can fix this by using optionals and then setting the image in the Attributes inspector in IB.
以下是更新后的代码:
import UIKit
@IBDesignable
class MyView: UIView {
@IBInspectable
var anImage: UIImage? = UIImage(named: "image_one")
}
这篇关于访问@IBInspectable UIImage时,接口构建器代理崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!