Harshad Pansuriya
Harshad Pansuriya

Reputation: 20960

How to change the Size of collectionView in iOS

I am new to iOS and Xamarin. I have Create CollectionView with full Width and Height of the Main Frame. Now I want to Change the CollectionView size with 150 Height and full Width to parent with Horizontal Scroll. I try to change the code but it not work.

CollectionView with Full Screen :

 UICollectionViewDataSource source;
        UICollectionViewFlowLayout layout;
        UICollectionView collectionView;

    protected ViewController(IntPtr handle) : base(handle)
    {
        layout = new UICollectionViewFlowLayout
        {
            SectionInset = new UIEdgeInsets(20, 5, 5, 5),
            MinimumInteritemSpacing = 5,
            MinimumLineSpacing = 5,
            ItemSize = new SizeF(100, 100),


        };

        collectionView = new UICollectionView(UIScreen.MainScreen.Bounds, layout);
        collectionView.ContentSize = new CGSize(View.Frame.Size.Width, View.Frame.Size.Height);
        collectionView.BackgroundColor = UIColor.White;
        source = new UICollectionViewDataSource();

        collectionView.RegisterClassForCell(typeof(TextCell), TextCell.CellId);
        collectionView.Source = source;
    }

Output :

enter image description here

I Expect :

Width = Parent Width,

Height = 150

So I change the code to this

layout = new UICollectionViewFlowLayout
            {
                SectionInset = new UIEdgeInsets(20, 5, 5, 5),
                MinimumInteritemSpacing = 5,
                MinimumLineSpacing = 5,
                ItemSize = new SizeF(100, 100),


            };

            collectionView = new UICollectionView(UIScreen.MainScreen.Bounds, layout);
            collectionView.ContentSize = new CGSize(View.Frame.Size.Width, 150);
            collectionView.BackgroundColor = UIColor.White;
            source = new UICollectionViewDataSource();

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

but also it display in Full Screen.

Any Help be Appreciated.

Upvotes: 1

Views: 1556

Answers (1)

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20960

Finally I Create One View and add CollectionView as SubView and finally It work as I expected with Full Width and 150 Height.

Working Code :

UICollectionView collectionView;
        UICollectionViewDataSource source;
        UICollectionViewFlowLayout layout;



        protected ViewController(IntPtr handle) : base(handle)
        {
            layout = new UICollectionViewFlowLayout
            {
                SectionInset = new UIEdgeInsets(20, 10, 5, 5),
                MinimumInteritemSpacing = 5,
                MinimumLineSpacing = 5,
                ItemSize = new SizeF(120, 130),
                ScrollDirection = UICollectionViewScrollDirection.Horizontal
            };

            UIView topcontainer = new UIView();

            topcontainer.Frame = new CGRect(0f, 0f, View.Frame.Width, 150f);

            View.AddSubview(topcontainer);

            collectionView = new UICollectionView(topcontainer.Bounds, layout);
            collectionView.ContentSize = new CGSize(topcontainer.Frame.Size.Width, topcontainer.Frame.Size.Height);
            collectionView.CollectionViewLayout = layout;
            collectionView.BackgroundColor = UIColor.White;
            source = new UICollectionViewDataSource();

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

            topcontainer.AddSubview(collectionView);
        }

Output :

enter image description here

Upvotes: 1

Related Questions