Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

ios - UICollectionVIew: How can I remove the space on top first cell's row?

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

enter image description here

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

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

question from:https://stackoverflow.com/questions/23786198/uicollectionview-how-can-i-remove-the-space-on-top-first-cells-row

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)];

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...