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

查看:71
本文介绍了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;

但是我没有运气.

我如何摆脱这个空间?

推荐答案

UICollectionView是具有 contentInset 属性的UIScrollView类的后代,设置-20顶部插入可解决此问题

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,它允许视图控制器根据状态栏,导航栏以及工具栏或选项卡栏占用的屏幕区域来调整其滚动视图插图.如果您要自己管理滚动视图插图调整,请设置为否".

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