简体   繁体   English

以编程方式用 UIImage 填充 UIImageView 并放置在 UICollectionViewCell 中

[英]Programmatically fill UIImageView with UIImage and place in UICollectionViewCell

I have been trying to fill in a UIImageView with a UIImage (car system image).我一直在尝试用UIImage (汽车系统图像)填充UIImageView I have tried numerous solutions online but can not seem to have it fill the UICollectionViewCell , awkward spacing seen here:我在网上尝试了许多解决方案,但似乎无法填充UICollectionViewCell ,此处看到的间距尴尬:

在此处输入图片说明

I figured since the UIView is the size of the UICollectionViewCell as seen here:我想,因为UIView是 UICollectionViewCell 的大小,如下所示:

在此处输入图片说明

It must be the image is not scaling to the view.它必须是图像没有缩放到视图。 So I modified the content mode to scale to fill of the image view when creating the cells for the collection view, then adding this image view into the collection view cell:因此,我在为集合视图创建单元格时修改了内容模式以缩放以填充图像视图,然后将此图像视图添加到集合视图单元格中:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CarCell", for: indexPath)
    
    let carImageView = UIImageView(image: UIImage(systemName: "car"))
    
    carImageView.contentMode = .scaleToFill
    cell.addSubview(carImageView)
    
    return cell
    
}

This doesn't seem to have any effect (even when using .bottom for the content mode the image does not move), but looking in the view hierarchy it seems this is what should be fixing it.这似乎没有任何效果(即使使用 .bottom 作为内容模式图像也不会移动),但在视图层次结构中看起来这应该是修复它的原因。

Does anyone know what I may be doing wrong?有谁知道我可能做错了什么?

Here is the storyboard:这是故事板:

在此处输入图片说明

Update:更新:

I have added constraints to the image view with this code:我使用以下代码向图像视图添加了约束:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CarCell", for: indexPath)
    
    let carImage = UIImage(systemName: "car")
    
    let carImageView = UIImageView(image: carImage)
    carImageView.contentMode = .scaleToFill
    
    cell.addSubview(carImageView)
    
    carImageView.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
    carImageView.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
    carImageView.leftAnchor.constraint(equalTo: cell.leftAnchor).isActive = true
    carImageView.rightAnchor.constraint(equalTo: cell.rightAnchor).isActive = true
    
    return cell
    
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
    let columns: CGFloat = 2
    
    let collectionViewWidth = collectionView.bounds.width
    let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    let spaceBetweenCells = flowLayout.minimumInteritemSpacing * (columns - 1)
    
    let adjustedWidth = collectionViewWidth - spaceBetweenCells
    
    let width: CGFloat = floor(adjustedWidth / columns)
    let height: CGFloat = width
    
    return CGSize(width: width, height: height)
    
}

But it ends up shrinking the image view rather than growing it to the size of the cell:但它最终缩小了图像视图,而不是将其增大到单元格的大小:

在此处输入图片说明

在此处输入图片说明

Give constraint to your imageView is zero from all side of collectionView cell and set image content mode to .scaleToFill and than show the result.给约束您的ImageView是zero ,从细胞的CollectionView和设定的图像内容模式的所有侧.scaleToFill比显示的结果。

Thanks谢谢

I got you there.我让你在那里。 As I previously commented to show how you are giving constraints to image view, thus the error was there.正如我之前评论显示您如何对图像视图施加约束,因此错误就在那里。 There is a property called "translatesautoresizingmaskintoconstraints" which you need set while giving constraints to your views.有一个名为“translatesautoresizingmaskintoconstraints”的属性,您需要在对视图施加约束的同时对其进行设置。 You can read about it here .你可以在这里阅读它。

Now, in your cellForItemAt you need to change:-现在,在您的cellForItemAt 中,您需要更改:-

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CarCell", for: indexPath)

let carImage = UIImage(systemName: "car")

let carImageView = UIImageView(image: carImage)
carImageView.contentMode = .scaleToFill

carImageView.translatesautoresizingmaskintoconstraints = false

cell.addSubview(carImageView)

carImageView.centerXAnchor.constraint(equalTo: cell.contentView.centerXAnchor).isActive = true
carImageView.centerYAnchor.constraint(equalTo: cell.contentView.centerYAnchor).isActive = true
carImageView.heightAnchor.constraint(equalTo: cell.contentView.heightAnchor).isActive = true
carImageView.widthAnchor.constraint(equalTo: cell.contentView.widthAnchor).isActive = true

return cell

}

As you can notice I've also changed the constraint to give an idea about how you should give the constraints if you want to set the image size as exact as your cell.正如您所注意到的,我还更改了约束,以了解如果您想将图像大小设置为与您的单元格一样精确,您应该如何设置约束。 You should always access cell's content view when you're giving constraint not the cell.当您提供约束而不是单元格时,您应该始终访问单元格的内容视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 UICollectionViewCell 中锚定 UIImageVIew(以编程方式) - Unable to Anchor UIImageVIew in UICollectionViewCell (programmatically) 如何以编程方式修复带有UIImageView Overlap的UICollectionViewCell - How to fix UICollectionViewCell with UIImageView Overlap Programmatically 用动画填充完整UIImage中的UIImageView的百分比 - Fill percent of UIImageView from full UIImage with animation 以编程方式调整UICollectionViewCell的视图的位置在哪里? - Where is the place to programmatically adjust the view of a UICollectionViewCell? Swift以编程方式将UIImageView放在另一个上面并调整大小 - Swift programmatically place UIImageView on top of another and resize 如何以编程方式将 UILabel 放置在 UIImageview 上? - How do I programmatically place UILabel on UIImageview? 尝试将UIView放在UIImageView的上方以突出显示UIImage的一部分 - Trying to place a UIView over top of a UIImageView to highlight a section of UIImage 如何在“方面填充”模式下将 UIImageView 裁剪为新的 UIImage? - How to crop a UIImageView to a new UIImage in 'aspect fill' mode? 将UIImageView触摸坐标转换为UIImage坐标宽高比填充 - Converting UIImageView Touch Coordinates to UIImage Coordinates Aspect Fill 在UICollectionViewCell中隐藏UIImage - Hiding UIImage in UICollectionViewCell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM