集合视图列之间的间距因屏幕尺寸iOS而异

时间:2016-10-27 04:49:50

标签: ios iphone xamarin uicollectionview uicollectionviewcell

我正在制作UICollectionView张图片。我不希望细胞之间有任何间距。为此,我执行以下操作: -

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);
}

使用这段代码我在iPhone 5S屏幕上的单元格看起来很好但是当我在病房的iPhone 6上测试时,单元格之间的间距太大,如下所示:

iPhone 5s

iPhone 6

如何解决这个问题,并且在细胞之间根本没有空间?

1 个答案:

答案 0 :(得分:1)

如果您希望所有设备在行中有4个单元格,则根据设备宽度设置单元格大小。

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);
}