ios UICollectionVIew:如何删除第一个单元格行顶部的空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23786198/
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-31 00:10:41  来源:igfitidea点击:

UICollectionVIew : How can i remove the space on top first cell's row

iosuicollectionviewuicollectionviewlayout

提问by SaintTail

I have this arrangement in Interface Builder, All properties are set to zero.

我在 Interface Builder 中有这种安排,所有属性都设置为零。

enter image description here

enter image description here

However, when I run it on both device and simulator it appears like this

但是,当我在设备和模拟器上运行它时,它看起来像这样

enter image description here

enter image description here

Where is the space above the cells come from?

单元格上方的空间从何而来?

So I try to set these properties for UICollectionViewFlowLayout in code like this

所以我尝试在这样的代码中为 UICollectionViewFlowLayout 设置这些属性

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.headerReferenceSize = CGSizeZero;
    layout.footerReferenceSize = CGSizeZero;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(103, 119);

    self.calendarView.collectionViewLayout = layout;

but I have no luck.

但我没有运气。

How can I get rid of that space? Thanks.

我怎样才能摆脱那个空间?谢谢。

回答by Seryozha

UICollectionView is descendant of UIScrollView class which has contentInsetproperty, setting -20 top inset fixes the problem

UICollectionView 是具有contentInset属性的 UIScrollView 类的后代,设置 -20 top inset 解决了这个问题

[self.calendarView setContentInset:UIEdgeInsetsMake(-20, 0, 0, 0)];

However,the problem comes from UIViewController's automaticallyAdjustsScrollViewInsetsproperty. By documentation:

但是,问题来自 UIViewController 的automaticallyAdjustsScrollViewInsets属性。通过文档:

Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself.

默认值为 YES,它允许视图控制器根据状态栏、导航栏和工具栏或选项卡栏消耗的屏幕区域调整其滚动视图插入。如果您想自己管理滚动视图插入调整,请设置为 NO。

That's why we get adjusted content insets for status bar. It's better to disable automatically adjusting than manually set value which doesn't match in result picture.

这就是我们为状态栏调整内容插入的原因。关闭自动调整比手动设置的值更好,结果图片中不匹配。

[self setAutomaticallyAdjustsScrollViewInsets:NO];

回答by Yuchen Zhong

Another way is to select your ViewControllerand uncheck the checkbox Adjust Scroll View Insetsin your interface builder:

另一种方法是选择您的ViewController并取消选中您的界面构建器中的“调整滚动视图插入”复选框:

enter image description here

enter image description here

It is essentially the same as the following line of code. But you got to see your changes right away in the interface builder.

它与以下代码行基本相同。但是您必须立即在界面构建器中看到您的更改。

automaticallyAdjustsScrollViewInsets = false

回答by Rob Norback

Here is the answer in swift with a few adjustments:

这是快速的答案,并进行了一些调整:

I have a collection view that takes up a small portion of the view. I used:

我有一个集合视图,它占据了视图的一小部分。我用了:

self.automaticallyAdjustsScrollViewInsets = false

to remove the top spacing that was messing up my layout. This piece of code didn't work for me:

删除弄乱我的布局的顶部间距。这段代码对我不起作用:

self.paperCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)

And neither did this one:

而这个也没有:

self.paperCollectionView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)

But that might be because I'm not using a UICollectionViewController, I'm just using a UICollectionView.

但这可能是因为我没有使用 UICollectionViewController,我只是在使用 UICollectionView。

Here's a bigger portion of the code to give more context: enter image description here

这是代码的更大部分以提供更多上下文: enter image description here

回答by Bogdan Razvan

iOS 11 deprecated the use of automaticallyAdjustsScrollViewInsets, so the use of collectionView.contentInsetAdjustmentBehavior = .neveris advised.

iOS 11 已弃用automaticallyAdjustsScrollViewInsets,因此collectionView.contentInsetAdjustmentBehavior = .never建议使用。

回答by Kirtikumar A.

You can also go with

你也可以去

[self.calendarView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[self setAutomaticallyAdjustsScrollViewInsets:NO];

There is by default collection view header scrolling space added on the collection view and you do not need to add -20 from top because it may reflect on device issue

默认情况下,在集合视图上添加了集合视图标题滚动空间,您不需要从顶部添加 -20,因为它可能反映设备问题

回答by Urvish Modi

Swift 3:

斯威夫特 3:

self.automaticallyAdjustsScrollViewInsets = false

回答by elliott.io

You can do this in Interface Builder by going to the Scroll View section and changing the Content insets dropdown to "Never".

您可以在 Interface Builder 中通过转到 Scroll View 部分并将 Content insets 下拉列表更改为“Never”来执行此操作。

UICollectionView ScrollView ContentInset Never

UICollectionView ScrollView ContentInset Never

回答by Mark A. Durham

This answer is weird, but it works if you are working in Interface Builder and have a Collection View embedded in a View Controller that is under the control of a Tab Bar Controller that is the root view controller of a Navigation Controller.

这个答案很奇怪,但是如果您在 Interface Builder 中工作并且在视图控制器中嵌入了一个集合视图,该视图控制器受作为导航控制器根视图控制器的选项卡栏控制器的控制,则它有效。

  • Add a Toolbar to the View Controller that has the Collection View
  • Move the Toolbar in the hierarchy such that it is above the Collection View
  • 将工具栏添加到具有集合视图的视图控制器
  • 在层次结构中移动工具栏,使其位于集合视图上方

If the Toolbar is above the Collection View, there will be no space from the top of the prototype Collection View Cell to the Collection View. If there is no Toolbar or the Toolbar is below the Collection View, then there will be space between the top of the Collection View and the Collection View Cell. This is true both in the Storyboard preview and while running the app. The same type of thing occurs for Table Views.

如果 Toolbar 在 Collection View 之上,那么从原型 Collection View Cell 的顶部到 Collection View 之间将没有空间。如果没有 Toolbar 或者 Toolbar 在 Collection View 的下方,那么 Collection View 的顶部和 Collection View Cell 之间会有空间。在 Storyboard 预览和运行应用程序时都是如此。表视图也会发生相同类型的事情。

This was most recently tested with Xcode Version 8.3.3 Collection View Cells and Toolbar Dependency

这是最近使用 Xcode 版本 8.3.3 测试的 Collection View Cells and Toolbar Dependency