I want to put a UICollectionView control that shows thumbs horizontally (only a single line of thumbs). For some reason the UICollectionView push the thumbs 44 pixels down, so the "0" height is actually "44". I assume it might be adding this space to consider the navigation bar height (I just assume). Since my UICollectionView is only on part of the screen, I don't want this margin. Is there a way to remove it?
-
similar woe ? http://stackoverflow.com/questions/24916006/in-a-uicollectionview-the-cells-will-not-sit-on-the-bottom – Fattie Jul 23 '14 at 16:41
9 Answers
The issue may be in collection view's content insets.
Try to add self.automaticallyAdjustsScrollViewInsets = NO;
into view controller's viewDidLoad
method.

- 7,887
- 2
- 23
- 43
-
3Works great. It's very strange this thing cannot be addressed on the storyboard. – bashan Oct 19 '13 at 19:12
-
10
-
3
-
12@bashan It can be done on storyboard, there is a checkbox named "Adjust Scroll View Insets" – aryaxt May 11 '15 at 16:58
-
I have been working for the same issue since one and half day. I was trying to find the reason. But this worked for me like a charm. Answer really deserves upvotes. Thanks and upvoted !!! – NSPratik Jul 02 '15 at 06:49
-
-
11Note that if you are using an embed segue, and your collection view controller is the embedded controller, you need to call this method to the PARENT controller, and not inside your UICollectionViewController – csotiriou Jul 16 '15 at 19:57
-
-
Apple??? Real strange. They should handle this better. Thank you for the answer, saved me a lot of time! – zumzum Jan 24 '16 at 06:08
-
1i had a tab bar controller added too. I selected by TabBarController in the storyboard and Unchecked ```Under Top Bars``` of ```Extend Edges```. I also unchecked ```Adjust Scroll View Insets``` in both TabBarController and my CollectionView and it worked perfectly – kishorer747 Jun 10 '16 at 13:44
-
-
@RamySabry, https://developer.apple.com/reference/uikit/uiviewcontroller/1621372-automaticallyadjustsscrollviewin – Sviatoslav Yakymiv Feb 06 '17 at 10:52
-
1
-
2
-
@VanDuTran did u get relevent ans for iOS 11 ?? also fetching same issue. – Tushar Lathiya Aug 06 '18 at 09:59
-
this is the correct answer - regardless if you do it programmatically or via storyboard – iDoc Dec 02 '19 at 15:17
-
You can set this in the storyboard too.
Make sure you've selected the ViewController, and then untick "Adjust Scroll View Insets".
I haven't tested what this IB/Storyboard method does on iOS6 though. With the code method you need to check that the VC responds to the method:
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}

- 4,880
- 1
- 25
- 34
I found that adding:
self.edgesForExtendedLayout = UIRectEdgeNone;
In the view controller I was loading the UICollectionView in solved the problem as I couldn't get the accepted answer to work.
The question I found this answer to can be found here and provides an extremely in-depth and interesting explanation of the difference between automatically adjusted scrolled view insets, extended layouts and edge for extended layouts.
Well worth a read
Swift 3:
First you want to set the viewControllers automaticallyAdjustsScrollViewInsets
to false:
self.automaticallyAdjustsScrollViewInsets = false
Then, you should be able to adjust the edge insets accordingly:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
//top, left, bottom, right
return UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
}

- 5,170
- 10
- 41
- 71

- 4,694
- 7
- 54
- 98
Maybe you can try to force this value at 0 using the Delegate flow layout of collection view :
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(PADDING_TOP, PADDING_LEFT, PADDING_BOTTOM, PADDING_RIGHT);
}
Modify the value of your padding.

- 8,227
- 2
- 35
- 40
As some others mentioned, viewController.automaticallyAdjustsScrollViewInsets
has been deprecated since iOS 11. My solution...
Swift 4.2, Xcode 10.1, iOS 12.1:
For some reason, collectionView.contentSize.height
was appearing smaller than the resolved height of my collection view. First, I was using an auto-layout constraint relative to 1/2 of the superview's height. To fix this, I changed the constraint to be relative to the "safe area" of the view.
This allowed me to set the cell height to vertically fill my collection view using collectionView.contentSize.height
:
private func setCellSize() {
let height: CGFloat = (collectionView.contentSize.height) / CGFloat(numberOfRows)
let width: CGFloat = view.frame.width - CGFloat(horizontalCellMargin * 2)
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSize(width: width, height: height)
}
Before
After

- 1,346
- 16
- 18
Swift 5.2 / Xcode 11.3
viewController.automaticallyAdjustsScrollViewInsets
has been deprecated since iOS 11.
My solution:
collectionView.contentInsetAdjustmentBehavior = .never

- 6,306
- 3
- 19
- 20
Try using self.feedCollectionView.contentInsetAdjustmentBehavior = .never
on the collection view. This actually worked for me.

- 722
- 1
- 7
- 17
Similar to @Sviatoslav answer, you can try the following:
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
yourCollectionView.contentOffset = CGPointMake(0, 0);
}

- 1,310
- 3
- 18
- 26