I have created a CollectionView with three items per row. If my cell includes just a view its working really fine like the picture shows:

But if I add an Imageview inside my view with equal constraints its going to look like this:

Here is my Code for my 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)
}
Seems like you are using a UICollectionViewFlowLayout.
This is not a bug or unexpected behavior. If there is something constrained to the edges of a cell's contentView, the layout will resize the cell to fit the contents. Instead of constraining the image to the leading/trailing and top/bottom anchors, constrain its center to the cell's center, this way it will not affect the cell's size. You may leave collectionView(_:layout:sizeForItemAt:) as is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With