UICollectionVIew:如何删除顶部第一个单元格行上的空格 [英] UICollectionVIew : How can i remove the space on top first cell's row
问题描述
我在Interface Builder中有这种安排,所有属性都设置为零。
I have this arrangement in Interface Builder, All properties are set to zero.
然而,当我在设备和模拟器上运行它时,它看起来像这样
However, when I run it on both device and simulator it appears like this
单元格上方的空间来自哪里?
Where is the space above the cells come from?
所以我尝试在这样的代码中为UICollectionViewFlowLayout设置这些属性
So I try to set these properties for UICollectionViewFlowLayout in code like this
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;
但我没有运气。
我该怎样摆脱那个空间?
谢谢。
How can I get rid of that space? Thanks.
推荐答案
UICollectionView是UIScrollView类的后代,它具有 contentInset
property,设置-20 top inset修复问题
UICollectionView is descendant of UIScrollView class which has contentInset
property, setting -20 top inset fixes the problem
[self.calendarView setContentInset:UIEdgeInsetsMake(-20, 0, 0, 0)];
然而,问题来自UIViewController的 automaticAdjustsScrollViewInsets
属性。
通过文档:
However, the problem comes from UIViewController's automaticallyAdjustsScrollViewInsets
property.
By documentation:
默认值为YES,允许视图控制器调整其滚动视图插入以响应屏幕状态栏,导航栏和工具栏或标签栏消耗的区域。如果您想自己管理滚动视图插入调整,请设置为NO。
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.
这就是我们为状态栏调整内容插入的原因。最好禁用自动调整而不是手动设置的值,这与结果图片中的不匹配。
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];
这篇关于UICollectionVIew:如何删除顶部第一个单元格行上的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!