当 ImageView 在其中时,Swift CollectionView 会更改大小

3
我创建了一个每行三个项目的CollectionView。如果我的单元格只包含一个视图,那么它的效果非常好,就像图片所示: here its shows like it should 但是,如果我在我的视图中添加一个等约束的Imageview,它将会变成这样: Collection view cell with imageview 这是我的CollectionView代码:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets.zero
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}     

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let yourWidth = collectionView.bounds.width/3.0
    let yourHeight = yourWidth

    return CGSize(width: yourWidth, height: yourHeight)
}

你是否将collectionView的代理设置为你的控制器? - undefined
你需要启用图像视图的"剪裁边界"设置。 - undefined
1个回答

0

看起来你正在使用 UICollectionViewFlowLayout

这不是一个错误或意外的行为。如果有一些内容被限制在单元格的 contentView 的边缘上,布局会调整单元格的大小以适应内容。而不是将图像限制在前导/尾随和顶部/底部锚点上,将其中心约束到单元格的中心,这样它就不会影响单元格的大小。你可以保留 collectionView(_:layout:sizeForItemAt:) 不变。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接