集合视图布局问题
Collection View Layout Problems
我正在使用 CollectionViews
,目前遇到的问题是每次我实施 UICollectionViewFlowLayout
时,我的原始布局都会被“破坏”。如果我不实现 FlowLayout,我的 CollectionView
只是一行,其中包含所有项目的长度,并且一切正常。但是 CollectionView
中的元素之间的距离真的很远。要使用更小的间距,我需要实现 FlowLayout,但当我这样做时,我的 CollectionView
使用网格布局并且其中的每个元素都缩小到大约 20x20 像素。
在哪里告诉 FlowLayout 只使用一行以及如何减小 CollectionView
中元素之间的间距?我看到了类似
ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical)
{
ItemSpacing = 20
}
但我不知道该把代码放在哪里。有人能帮我吗? :/
这就是我当前的 FlowLayout 的样子:
public class TestFlowLayout : UICollectionViewFlowLayout
{
public TestFlowLayout()
{
}
public override bool ShouldInvalidateLayoutForBoundsChange(CGRect newBounds)
{
return true;
}
public override UICollectionViewLayoutAttributes LayoutAttributesForItem(NSIndexPath indexPath)
{
return base.LayoutAttributesForItem(indexPath);
}
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
{
return base.LayoutAttributesForElementsInRect(rect);
}
public override void PrepareLayout()
{
base.PrepareLayout();
}
public override void InvalidateLayout()
{
base.InvalidateLayout();
}
}
我也在 ViewDidLoad
:
中尝试过类似的方法
CollectionView.CollectionViewLayout = new TestFlowLayout
{
MinimumLineSpacing = 0,
MinimumInteritemSpacing = 0,
SectionInset = new UIEdgeInsets(0, 0, 0, 0),
ScrollDirection = UICollectionViewScrollDirection.Horizontal
};
但我仍然得到网格布局而不是线性布局。
我也加了
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
在我的 UICollectionViewDataSource 中。但是没有任何效果..
原因:如果UICollectionView在一个方向上有多个Cell space,它会让Cell填充。比如我们设置UICollectionView的滑动方向为水平。此时,如果Cell的高度不够,可能会出现以下情况:
解法:
一般来说,要显示单行(单列),只需要让Cell的高度大于UICollectionView高度的1/2即可。那么一行不够放两个cell,自然会出现单行排列:
因此在您的情况下,您只需要设置 UICollectionView 的框架。
我正在使用 CollectionViews
,目前遇到的问题是每次我实施 UICollectionViewFlowLayout
时,我的原始布局都会被“破坏”。如果我不实现 FlowLayout,我的 CollectionView
只是一行,其中包含所有项目的长度,并且一切正常。但是 CollectionView
中的元素之间的距离真的很远。要使用更小的间距,我需要实现 FlowLayout,但当我这样做时,我的 CollectionView
使用网格布局并且其中的每个元素都缩小到大约 20x20 像素。
在哪里告诉 FlowLayout 只使用一行以及如何减小 CollectionView
中元素之间的间距?我看到了类似
ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical)
{
ItemSpacing = 20
}
但我不知道该把代码放在哪里。有人能帮我吗? :/
这就是我当前的 FlowLayout 的样子:
public class TestFlowLayout : UICollectionViewFlowLayout
{
public TestFlowLayout()
{
}
public override bool ShouldInvalidateLayoutForBoundsChange(CGRect newBounds)
{
return true;
}
public override UICollectionViewLayoutAttributes LayoutAttributesForItem(NSIndexPath indexPath)
{
return base.LayoutAttributesForItem(indexPath);
}
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
{
return base.LayoutAttributesForElementsInRect(rect);
}
public override void PrepareLayout()
{
base.PrepareLayout();
}
public override void InvalidateLayout()
{
base.InvalidateLayout();
}
}
我也在 ViewDidLoad
:
CollectionView.CollectionViewLayout = new TestFlowLayout
{
MinimumLineSpacing = 0,
MinimumInteritemSpacing = 0,
SectionInset = new UIEdgeInsets(0, 0, 0, 0),
ScrollDirection = UICollectionViewScrollDirection.Horizontal
};
但我仍然得到网格布局而不是线性布局。
我也加了
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
在我的 UICollectionViewDataSource 中。但是没有任何效果..
原因:如果UICollectionView在一个方向上有多个Cell space,它会让Cell填充。比如我们设置UICollectionView的滑动方向为水平。此时,如果Cell的高度不够,可能会出现以下情况:
解法:
一般来说,要显示单行(单列),只需要让Cell的高度大于UICollectionView高度的1/2即可。那么一行不够放两个cell,自然会出现单行排列:
因此在您的情况下,您只需要设置 UICollectionView 的框架。