子类视图的 uiimage 未显示为 IBDesignable
Subclassed view's uiimage not appearing as IBDesignable
我已经设置了一个子类 UIView
,并希望在 IB 中看到嵌入的图像 - 所以我将其设置为 IBDesignable
@IBDesignable
class DieView: UIView {
@IBInspectable
var dieImage : UIImage = UIImage()
override init(frame: CGRect) {
super.init(frame: frame)
updateLayout()
}
convenience init() {
self.init(frame: CGRect.zero)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
updateLayout()
}
// for IB
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
updateLayout()
}
func updateLayout() {
self.backgroundColor = .red
let profileImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))
profileImageView.image = UIImage(named: "dice1")
profileImageView.contentMode = .scaleAspectFit
profileImageView.layer.masksToBounds = false
self.addSubview(profileImageView)
}
func showNumber(number: Int) {
}
}
背景颜色改变,但嵌入的图像没有更新。为什么不呢?
来自prepareForInterfaceBuilder()
的documentation:
Interface Builder waits until all objects in a graph have been created
and initialized before calling this method. So if your object’s
runtime configuration relies on subviews or parent views, those
objects should exist by the time this method is called.
表示在调用此方法之前子视图应该存在。我不确定,但尝试在调用之前添加图像视图。此外,您必须记住 prepareForInterfaceBuilder()
由界面构建器独立调用。阅读文档以获取更多信息。祝你好运!
我已经设置了一个子类 UIView
,并希望在 IB 中看到嵌入的图像 - 所以我将其设置为 IBDesignable
@IBDesignable
class DieView: UIView {
@IBInspectable
var dieImage : UIImage = UIImage()
override init(frame: CGRect) {
super.init(frame: frame)
updateLayout()
}
convenience init() {
self.init(frame: CGRect.zero)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
updateLayout()
}
// for IB
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
updateLayout()
}
func updateLayout() {
self.backgroundColor = .red
let profileImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))
profileImageView.image = UIImage(named: "dice1")
profileImageView.contentMode = .scaleAspectFit
profileImageView.layer.masksToBounds = false
self.addSubview(profileImageView)
}
func showNumber(number: Int) {
}
}
背景颜色改变,但嵌入的图像没有更新。为什么不呢?
来自prepareForInterfaceBuilder()
的documentation:
Interface Builder waits until all objects in a graph have been created and initialized before calling this method. So if your object’s runtime configuration relies on subviews or parent views, those objects should exist by the time this method is called.
表示在调用此方法之前子视图应该存在。我不确定,但尝试在调用之前添加图像视图。此外,您必须记住 prepareForInterfaceBuilder()
由界面构建器独立调用。阅读文档以获取更多信息。祝你好运!