简体   繁体   English

如何在Swift中将背景图像添加到UICollectionViewCell?

[英]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: 我见过这些类似的问题:

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.

相关问题 如何将UICollectionViewCell中的图像显示到另一个UIImageView? (快速3) - How to show the image in the UICollectionViewCell to another UIImageView? (Swift 3) 如何在UIView中显示所选UICollectioNViewCell的图像? (快速3) - How to display image of selected UICollectioNViewCell in UIView? (Swift 3) 如何以编程方式在Swift 3中将UILabel添加到UICollectionViewCell - How to programmatically add a UILabel to UICollectionViewCell in Swift 3 如何为具有清晰背景颜色的 UICollectionViewCell 添加阴影? - How to add shadow to UICollectionViewCell with clear background color? 如何将 UITapGestureRecognizer 添加到 UICollectionViewCell 中的图像 - How to add UITapGestureRecognizer to an image within UICollectionViewCell Swift:如何将图标图像和图像背景添加到 App 委托中的 UItabBar? - Swift : How to add icon image and image background to the UItabBar in App delegate? Swift - 为 UICollectionViewCell 选择图像和 label - Swift - pick image and label for UICollectionViewCell UICollectionViewCell背景图像超出单元格 - UICollectionViewCell background image out of cell 如何更改已点击的UICollectionViewCell的背景图像? - How do I change the background image of a UICollectionViewCell that has been tapped? Swift如何在uicollectionviewcell的子元素上添加点击手势 - Swift how to add tap gesture on uicollectionviewcell's child elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM