UICollectionVIew : How can i remove the space on t

2019-03-08 13:49发布

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

7条回答
够拽才男人
2楼-- · 2019-03-08 14:07

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

enter image description here

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楼-- · 2019-03-08 14:07

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

UICollectionView ScrollView ContentInset Never

查看更多
三岁会撩人
4楼-- · 2019-03-08 14:12

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 Collection View Cells and Toolbar Dependency

查看更多
来,给爷笑一个
5楼-- · 2019-03-08 14:13

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: enter image description here

查看更多
Evening l夕情丶
6楼-- · 2019-03-08 14:15

Swift 3:

self.automaticallyAdjustsScrollViewInsets = false
查看更多
淡お忘
7楼-- · 2019-03-08 14:16

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

查看更多
登录 后发表回答