Jim Layhey
Jim Layhey

Reputation: 363

Update UICollectionViewFlowLayout without creating a new UICollectionView?

I would like to update certain the UICollectionViewFlowLayout or a UICollectionView because I want to set the size of the cells to a specific number that needs to be calculated (screenwidth / 3) - 2. However, I only know how to assign it by creating a new instance which would reset lots of properties and force me to redefine them. Can this be updated some where else? Or is there a good way to just update ItemSize?

UICollectionViewFlowLayout layout = new UICollectionViewFlowLayout
{
      SectionInset = new UIEdgeInsets(0, 0, 0, 0),
      MinimumInteritemSpacing = 2,
      MinimumLineSpacing = 2,
      ItemSize = new SizeF(Size, Size),
      ScrollDirection = UICollectionViewScrollDirection.Vertical,
};
ProfilesListView = new UICollectionView(ProfilesListView.Frame, layout);

Upvotes: 0

Views: 135

Answers (1)

Ax1le
Ax1le

Reputation: 6641

There's no need to initialize UICollectionViewFlowLayout again when you want to set the item's size. You can use UICollectionViewDelegateFlowLayout to programmatically modify the size:

public class MyCollectionDelegate : UICollectionViewDelegateFlowLayout
{
    public override CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
    {
        return new CGSize(Size, Size);
    }
}

After setting this Delegate to your UICollectionView: ProfilesListView.Delegate = new MyCollectionDelegate();

You can modify it through changing the event GetSizeForItem() and forcing updating the UICollectionView ProfilesListView.ReloadData();

Upvotes: 1

Related Questions