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

查看:1385
本文介绍了在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中来实现这一点,方法是将其框架设置为模拟边框,但它在框架内滚动,因此在顶部和底部被切断,滚动条也出现在框架的边界内。

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天全站免登陆