我是iOS和Xamarin的新手。我创建了nameof(ClassName.IntProperty)
,其中包含主框架的完整CollectionView
和Width
。现在,我想将Height
150
和完整Height
的CollectionView大小更改为Width
的父级。我尝试更改代码但不起作用。
全屏CollectionView:
Horizontal Scroll
输出
我期待:
宽度=父宽度,
身高= 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;
}
但也以全屏显示。
任何帮助都要得到赞赏。
答案 0 :(得分:1)
最后,我创建一个View
并添加CollectionView
作为SubView,最后它按照我预期的方式使用完整Width
和150
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);
}
输出