如何在iOS中更改collectionView的大小

时间:2016-10-14 05:39:03

标签: ios xamarin.ios uicollectionview

我是iOS和Xamarin的新手。我创建了nameof(ClassName.IntProperty) ,其中包含主框架的完整CollectionViewWidth。现在,我想将Height 150和完整Height的CollectionView大小更改为Width的父级。我尝试更改代码但不起作用。

全屏CollectionView:

Horizontal Scroll

输出

enter image description here

我期待:

  

宽度=父宽度,

     

身高= 150

所以我将代码更改为

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

但也以全屏显示。

任何帮助都要得到赞赏。

1 个答案:

答案 0 :(得分:1)

最后,我创建一个View并添加CollectionView作为SubView,最后它按照我预期的方式使用完整Width150 Height

工作代码:

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

输出

enter image description here