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

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


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


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


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