我正在为项目使用Swift
。创建了一个自定义UIView
类,如下所示:
class AppBgView: UIView {
override init (frame : CGRect) {
super.init(frame : frame)
//self.isMemberOfClass(<#aClass: AnyClass#>)
}
convenience init () {
self.init(frame:CGRect.zeroRect)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor(patternImage: UIImage(named: "BGround.png")!)
}
}
试图在所需的init()
方法内设置背景图像。但是背景图片似乎没有变化。任何建议,将不胜感激。谢谢。
在此方法的每个方法中都设置一个断点,并查看正在调用哪个构造函数。您将需要移动线
self.backgroundColor = UIColor(patternImage: UIImage(named: "BGround.png")!)
到被调用的构造方法。
在Swift中,一个类必须至少具有一个指定的初始化程序。您可能需要从init()中删除单词convenience。
给此人一个镜头:UIColor(patternImage:UIImage(named:“ BGround.png”)!)。CGColor
最后我设法解决了。用此代码替换self.backgroundColor = UIColor(patternImage: UIImage(named: "BGround.png")!)
代码:
if let image = UIImage(named: "bground.png") {
self.layer.backgroundColor = UIColor(patternImage: image).CGColor
}
背景图像已添加到该视图的层。有用。谢谢大家。