CollectionView within a UIView layout issues

I've gotten an issue where I dynamically create a CollectionView programmatically based upon retrieved data from a service, create a subview programmatically, and place them on the page.

I ideally need the CollectionView to expand its parent subview to fit it all in, as this is then hosted in a scroll view for a page where multiple CollectionViews doing the same thing will be placed inside.

However, what seems to happen is it expands out to show half of the items, but then the CollectionView itself is scrollable, or when I make the CollectionView.ScrollEnabled = false; it shows the first few items as it would if it would scroll but wont let you see the rest.:

Please see my code below for where I am going wrong:

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
    layout = new UICollectionViewFlowLayout
        {
            SectionInset = new UIEdgeInsets(10, 10, 0, 10),
            MinimumInteritemSpacing = 10,
            MinimumLineSpacing = 10,
            ItemSize = new SizeF(200, 100),
            ScrollDirection = UICollectionViewScrollDirection.Vertical,
        };

        if (subView == null)
        {
            subView = new UIView()
            {
                ClipsToBounds = false
            };
        }

        collectionView = new UICollectionView(subView.Bounds, layout)
        {

        };
        collectionView.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0);
        //collectionView.ContentSize = subView.Bounds.Size;

        source = new CVSource();
        source.nvc = this;

        collectionView.RegisterClassForCell(typeof(ViewCell), ViewCell.CellId);
        collectionView.Source = source;
        GetCellData();

        subView.AddSubview(collectionView);

    }

    private async void GetCellData()
    {
        source.data.Clear();
        var data = await service.GetData();
        source.data.AddRange(data);
        collectionView.Source = source;
        collectionView.ReloadData();
        subView.Frame = new CGRect(subView.Frame.X, subView.Frame.Y, collectionView.ContentSize.Width, collectionView.ContentSize.Height);            collectionView.Frame = new CGRect(collectionView.Frame.X, collectionView.Frame.Y, collectionView.ContentSize.Width, collectionView.ContentSize.Height);
    }

Any help would be massively appreciated

Thanks

Sign In or Register to comment.