在 UICollectionView 的每个边缘周围放置一个边距 [英] Placing a margin around each edge of the UICollectionView

查看:32
本文介绍了在 UICollectionView 的每个边缘周围放置一个边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 UICollectionView 填充了项目时,它们总是像这样直接到达 UICollectionView 的边缘:

When the UICollectionView is populated with items they always go right to the edges of the UICollectionView like so:

---------------
|X X X X X X X|
|X X X X X X X|
|X X X X X X X|
|X X X X X X X|
|X X X X X X X|
|X X X X X X X|
---------------

我想像这样在每个边缘周围放置一个边距:

I would like to place a margin around each edge like so:

---------------
| X X X X X X |
| X X X X X X |
| X X X X X X |
| X X X X X X |
| X X X X X X |
| X X X X X X |
---------------

我试图通过将 UICollectionView 放置在其宿主 UIView 中来实现这一点,方法是将其 Frame 设置为模拟边框,但它在 Frame 内滚动,因此在顶部和底部被切断,并且滚动条也出现在框架的边界中.

I tried to achieve this by placing the UICollectionView inside its hosting UIView by setting its Frame to simulate a border but it scrolls within the Frame so gets cut off at the top and bottom and the scroller also appears in the bounds of the frame.

我查看了 API,但看不到任何明显可以实现此目的的东西.有什么想法吗?

I have looked at the API but I cannot see anything obvious to achieve this. Any ideas?

推荐答案

(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
}

您可以在左侧为单元格留出一些边距.

you can give your cells some margin on the left side.

或者您可以创建具有边距的自定义单元格.

Or you can create custom cells, which have a margin.

或者您可以设置 CollectionviewFlowLayout 的属性 .sectionInset,这应该是最简单的方法(如果您使用 FlowLayout)

or you can set the property .sectionInset of your CollectionviewFlowLayout, which should be the easiest way (if you use FlowLayout)

这篇关于在 UICollectionView 的每个边缘周围放置一个边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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