-->

UICollectionVIew : How can i remove the space on t

2019-03-08 13:34发布

问题:

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?

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? Thanks.

回答1:

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


回答2:

Another way is to select your ViewController and uncheck the checkbox Adjust Scroll View Insets in your interface builder:

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


回答3:

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.

Here's a bigger portion of the code to give more context:



回答4:

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



回答5:

Swift 3:

self.automaticallyAdjustsScrollViewInsets = false


回答6:

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.

  • 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.

This was most recently tested with Xcode Version 8.3.3



回答7:

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