我有一个从这个github repo ZLSwipeableView引导的火种克隆。
我想用我的xcassets中的图像替换默认背景颜色。到目前为止,我已经能够做一个模式图像,但这并不好,我需要以某种方式使用UIImageView创建一个backgroundImage。这就是我到目前为止所做的:
let cardView = CardView(frame: swipeableView.bounds)
if let image = UIImage(named: "Image") {
cardView.backgroundColor = UIColor(patternImage: image)
} else {
print("There was no such image.")
}
然后是CardView类
class CardView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
// Shadow
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowOpacity = 0.25
layer.shadowOffset = CGSizeMake(0, 1.5)
layer.shadowRadius = 4.0
layer.shouldRasterize = true
layer.rasterizationScale = UIScreen.mainScreen().scale
// Corner Radius
layer.cornerRadius = 10.0;
}
}
显然,每张卡片“框架”都设置为处理UIColor
类的颜色。如何将此CardView框架转换为UIImageView
对象,以便我可以设置backgroundImage?
答案 0 :(得分:0)
尝试将UIImageView作为子视图添加到CardView
var image:UIImage = UIImage(named: "Image.png")
var labelBackground = UIImageView(frame: CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>))
labelBackground = image
cardView.addSubview(labelBackground)