[英]How To Set Background Image with UIImageView in Swift
I have a tinder clone that I bootstrapped from this github repo ZLSwipeableView . 我有一个从这个github repo ZLSwipeableView引导的火种克隆 。
I want to replace the default background colors with images from my xcassets. 我想用我的xcassets中的图像替换默认的背景颜色。 So far I've been able to do a pattern image but that's no good, I need to somehow use UIImageView to create a backgroundImage. 到目前为止,我已经能够制作图案图像,但这并不好,我需要以某种方式使用UIImageView来创建backgroundImage。 This is what I have so far: 这是我到目前为止的内容:
let cardView = CardView(frame: swipeableView.bounds)
if let image = UIImage(named: "Image") {
cardView.backgroundColor = UIColor(patternImage: image)
} else {
print("There was no such image.")
}
Then the CardView class 然后是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;
}
}
Obviously each card "frame" is set up to handle colors with the UIColor
class. 显然,每个卡“框架”都设置为使用UIColor
类处理颜色。 How can I convert this CardView frame to a UIImageView
object so that I can set backgroundImage?
如何将CardView框架转换为UIImageView
对象,以便可以设置backgroundImage?
Try adding a UIImageView as a subview to your CardView 尝试将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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.