Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

UICollectionViewFlowLayout itemsize and spacing

OnnoJOnnoJ NLUniversity ✭✭

Hi there,

I'm trying to find out how the cell spacing and item sizes work when working with the UICollectionViewFlowLayout. In this particular example I'm trying to get 3 columns without any spacing in between the items. I'm grabbing the UICollectionView frame width and dividing it by 3 which should then be the item's width if I'm correct.

I've set the minimum line spacing to 0, as well as the item spacing and the section insets, to my knowledge I should end up with no spacing at all.

Here is my code:

    var collectionFrame = Collection.Bounds.Size;

        var itemWidth = collectionFrame.Width / 3;
        var itemHeight = itemWidth / 16 * 9;

        _source = new GenericCollectionViewSource<Room>(Collection, RoomCollectionCell.Identifier)
        {
            ModifyCellFunc =
            (view, path, arg3, arg4) =>
            {
                arg3.BackgroundColor = _lastItemWasRed ? UIColor.Blue : UIColor.Red;
                _lastItemWasRed = !_lastItemWasRed;
            }
        };
        Collection.RegisterClassForCell(typeof(RoomCollectionCell), RoomCollectionCell.Identifier);
        Collection.CollectionViewLayout = new UICollectionViewFlowLayout
        {
            MinimumInteritemSpacing = 0,
            MinimumLineSpacing = 0,
            ItemSize = new CGSize(itemWidth, itemHeight),
            SectionInset = new UIEdgeInsets(0, 0, 0, 0),
        };
        Collection.Source = _source;

But I always end up with lots of spacing in between the items, as show in this screenshot:

I think I am missing something here, can anyone please point me in the right direction?

Thanks
Onno

Answers

  • RonaldoButrusRonaldoButrus Member ✭✭

    I'm sorry that you haven't received any replies.
    Many people will have come across this problem, so it's worth putting out the solution:
    UICollectionViewFlowLayout.MinimumInterItemSpacing is the property to look for.

Sign In or Register to comment.