ios 在 UICollectionView 的每个边缘周围放置边距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16934831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:48:51  来源:igfitidea点击:

Placing a margin around each edge of the UICollectionView

iosuicollectionviewuicollectionviewlayout

提问by Adam

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

当 UICollectionView 填充有项目时,它们总是直接到达 UICollectionView 的边缘,如下所示:

---------------
|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 |
---------------

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.

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

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

我已经查看了 API,但我看不到任何明显的东西来实现这一点。有任何想法吗?

采纳答案by Heckscheibe

In

(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.

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

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

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

回答by Tyler Cloutier

I know this is an old question, but don't forget that UICollectionView inherits from UIScrollView.

我知道这是一个老问题,但不要忘记 UICollectionView 继承自 UIScrollView。

UICollectionView *collectionView = [[UICollectionView alloc] init];
collectionView.contentInset = UIEdgeInsetsMake(x.x, x.x, x.x, x.x);
// Swift 3 for good measure
let collectionView = UICollectionView()
collectionView.contentInset = UIEdgeInsets(top: x.x, left: x.x, bottom: x.x, right: x.x)

Be aware that both contentInset and sectionInset may change the spacing of cells in your view. For more information on that, see thispost.

请注意, contentInset 和 sectionInset 都可能会更改视图中单元格的间距。有关更多信息,请参阅帖子。

回答by Vinayak Kini

You can make use of the following function.

您可以使用以下功能。

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  {
      return UIEdgeInsetsMake(50, 50,15,50);
  }

You will have to play around with the number to figure out how to force the collectionviewCells in a single line.

您将不得不玩弄数字以弄清楚如何在一行中强制 collectionviewCells。

UIEdgeInsetsMake ( CGFloat top,CGFloat left,CGFloat bottom,CGFloat right); 

For Swift 3

对于 Swift 3

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(10, 4, 10, 4)
}

回答by mmackh

You can pretty much control every aspect of the grid with collectionview's protocol. Here's an example:

您几乎可以使用 collectionview 的协议来控制网格的各个方面。下面是一个例子:

- (UICollectionViewFlowLayout *)collectionViewFlowLayout
{
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.itemSize = CGSizeMake(180, 255);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 30, 0, 30);
    flowLayout.minimumInteritemSpacing = 0.0f;
    flowLayout.minimumLineSpacing = 0.0f;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    return flowLayout;
}

The one you would want to change in your case is the sectionInsets

在你的情况下你想改变的是 sectionInsets

回答by Tal Zion

Swift 2.0

斯威夫特 2.0

self.automaticallyAdjustsScrollViewInsets = false
self.collectionView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)

回答by Marcone

On Xcode 8, if you're using the interface builder you can set the Section Insets right on the Size Inspector:

在 Xcode 8 上,如果您使用的是界面构建器,您可以在 Size Inspector 上设置 Section Insets:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Skaal

Swift 4 updated(Vinayak Kinianswer) :

Swift 4 更新Vinayak Kini回答):

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}