UICollectionVIew:如何删除顶部第一个单元格行的空间? [英] UICollectionVIew: How can I remove the space on top first cell's row?

查看:12
本文介绍了UICollectionVIew:如何删除顶部第一个单元格行的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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?

推荐答案

UICollectionView 是具有 contentInset 属性的 UIScrollView 类的后代,设置 -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 的 automaticallyAdjustsScrollViewInsets 属性.通过文档:

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

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