如何居中对齐UICollectionView的单元格?
我目前正在使用 UICollectionView
作为用户界面网格,它运行正常。但是,我想启用水平滚动。网格每页支持8个项目,当项目总数为4时,这就是如何在启用水平滚动方向的情况下排列项目的方式:
I am currently using UICollectionView
for the user interface grid, and it works fine. However, I'd like to be enable horizontal scrolling. The grid supports 8 items per page and when the total number of items are, say 4, this is how the items should be arranged with horizontal scroll direction enabled:
0 0 x x
0 0 x x
这里0 - >收集物品
和x - >空单元
Here 0 -> Collection Item and x -> Empty Cells
有没有办法使它们居中对齐如下:
Is there a way to make them center aligned like:
x 0 0 x
x 0 0 x
这样内容看起来更干净了吗?
So that the content looks more clean?
以下安排也可能是我期待的解决方案。
Also the below arrangement might also be a solution I am expecting.
0 0 0 0
x x x x
我认为你可以通过实现这样的方式来实现单行外观:
I think you can achieve the single line look by implementing something like this:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 100, 0, 0);
}
你将不得不玩这个数字来弄清楚如何强迫内容成一行。第一个0是顶边参数,如果你想在屏幕中垂直居中,你也可以调整那个。
You will have to play around with that number to figure out how to force the content into a single line. The first 0, is the top edge argument, you could adjust that one too, if you want to center the content vertically in the screen.