43

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
SaintTail
  • 6,160
  • 4
  • 31
  • 51

8 Answers8

108

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];
Seryozha
  • 1,649
  • 1
  • 14
  • 13
  • Although it leads me to the correct answer. I have to set edge inset as (-20,0,0,0). Don't know why it is need. But Thanks. You can edit your answer so that i can up vote it. – SaintTail May 21 '14 at 14:42
  • 5
    Ok finally i know what causes this ... I have to set self.automaticallyAdjustsScrollViewInsets = NO; and it will work correctly. (Just notice it from the amount of inset which somehow equals to status bar height) – SaintTail May 22 '14 at 16:15
  • Thanks, we missed that. – Seryozha May 23 '14 at 07:44
  • In times of iPhone X and who knows what comes next, it's wrong to assume 20 as the height of the statusbar. Always refer to the actual height, to be safe here. – d4Rk Nov 20 '17 at 10:38
72

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
Yuchen
  • 30,852
  • 26
  • 164
  • 234
27

iOS 11 deprecated the use of automaticallyAdjustsScrollViewInsets, so the use of collectionView.contentInsetAdjustmentBehavior = .never is advised.

Bogdan Razvan
  • 1,497
  • 1
  • 16
  • 16
15

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

Rob Norback
  • 6,401
  • 2
  • 34
  • 38
  • Did you find the solution? I can see only the enhancement in question. I am having the same case using `UIViewController` not `UICollectionViewControllel` – umair151 May 15 '16 at 04:54
  • So I used `self.automaticallyAdjustsScrollViewInsets = false` and that took care of my spacing issue. Can you explain a bit more and perhaps I can help more? – Rob Norback May 16 '16 at 16:57
8

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

Kirtikumar A.
  • 4,140
  • 43
  • 43
2

Swift 3:

self.automaticallyAdjustsScrollViewInsets = false
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
Urvish Modi
  • 1,118
  • 10
  • 11
2

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

elliott.io
  • 31
  • 2
1

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

Mark A. Durham
  • 844
  • 1
  • 6
  • 18