如何居中对齐 UICollectionView 的单元格? [英] How to center align the cells of a UICollectionView?

查看:18
本文介绍了如何居中对齐 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

让内容看起来更干净?

下面的安排也可能是我期待的解决方案.

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 是 top edge 参数,如果你想在屏幕上垂直居中,你也可以调整那个.

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.

这篇关于如何居中对齐 UICollectionView 的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆