[英]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.