hello world hello world - 1 month ago 10
iOS Question

Spacing between collectionview coloumns differ on screen sizes iOS

I am creating a

UICollectionView
of Images. I do not want any spacing between the cells. To get this I do the following :-

public override nfloat GetMinimumInteritemSpacingForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
{

return 0;

}
public override nfloat GetMinimumLineSpacingForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
{

return 0;

}
public override CoreGraphics.CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, Foundation.NSIndexPath indexPath)
{
if (UIScreen.MainScreen.Bounds.Size.Height > 567)
{

return new CoreGraphics.CGSize(100, 100);
}
else
{
return new CoreGraphics.CGSize(80, 80);
}

}

public override UIEdgeInsets GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
{
return new UIEdgeInsets(10, 10, 10, 10);
}


With this code I the cells looks fine on a iPhone 5S screen but when I test it on a iPhone 6 on wards the cells have too much spacing between them like below:

iPhone 5s

iPhone 6

How can I solve this and achieve no space at all between the cells?

Answer

If you want all device have 4 cells in row then set cell size on the basis of device width.

public override CoreGraphics.CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, Foundation.NSIndexPath indexPath) {

   CoreGraphics.CGFloat width = (UIScreen.MainScreen.Bounds.Size.Width / 4)
   return new CoreGraphics.CGSize(width, width);
}