[英]How to add a background image to a UICollectionViewCell in Swift?
I had assumed that it would be easy to add a background image to a UICollectionViewCell
, but I am having trouble figuring it out. 我曾经假设将背景图像添加到
UICollectionViewCell
很容易,但我无法搞清楚它。 I have a border image like this that I want to add to every cell in my UICollectionView
. 我有一个这样的边框图像,我想添加到我的
UICollectionView
每个单元格。
border.png
I couldn't see an option to add it in the Interface Builder and I'm not sure which method to use on UICollectionViewCell
programmatically. 我看不到在Interface Builder中添加它的选项,我不确定以编程方式在
UICollectionViewCell
上使用哪种方法。
I have seen these similar questions: 我见过这些类似的问题:
UICollectionView
, not UICollectionViewCell
) UICollectionView
,而不是UICollectionViewCell
) I know this is a old question, with an accepted answer. 我知道这是一个老问题,接受了答案。 But why not just set the border of the cell like so:
但为什么不像这样设置单元格的边框:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.layer.borderWidth = 1.5
cell.layer.borderColor = UIColor.blueColor().CGColor
cell.layer.cornerRadius = 4
return cell
}
That way you don't need to use a image. 这样你就不需要使用图像了。 Just adjust the borderWidth and cornerRadius until you get the desired effect.
只需调整borderWidth和cornerRadius,直到获得所需的效果。
let View=UIView()
View.backgroundColor=UIColor(patternImage:UIImage(named:"border.png")!)
cell.backgroundView=View
in uicollectionview
cellForItemAtIndexPath
method 在
uicollectionview
cellForItemAtIndexPath
方法中
将UIImageView添加到CollectionViewCell并将此图像设置为图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.