0

Can anyone explain why UICollectionViewFlowLayout is adding extra space before the first section header in the following? The yellow border is the UICollectionView frame. The blue borders are the UICollectionReusableView section header frames:

enter image description here

The extra space is only appearing above the first section header, which is odd.

As far as I can tell, I'm zeroing everything out. This is how I instantiate the flow layout and collection view:

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = [BCFriendCell defaultSize];
flowLayout.minimumInteritemSpacing = 0.0f;
flowLayout.minimumLineSpacing = 0.0f;
flowLayout.headerReferenceSize = CGSizeMake(kScreenWidth, 63.0f);
flowLayout.footerReferenceSize = CGSizeZero;
flowLayout.sectionInset = UIEdgeInsetsZero;

CGRect collectionViewFrame = ...
collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:flowLayout];

collectionView.contentInset = UIEdgeInsetsZero;
[self.view addSubview:collectionView];

NSLog(@"contentOffset: %@", NSStringFromCGPoint(collectionView.contentOffset));

That last logging line prints out the expected

2014-09-30 19:23:28.974 MyApp[4468:60b] contentOffset: {0, 0}

Thanks for the help.

bcattle
  • 12,115
  • 6
  • 62
  • 82
  • 1
    Since the extra space only exists on the first section header it looks like it's a layout issue with the UICollectionView, not the header. The amount of extra space looks almost the same height as the status bar height. Check the constraints on the UICollectionView. – Brian Shamblen Oct 01 '14 at 02:53
  • I created the `UICollectionView` programmatically, are there default constraints that get applied? Where are they? – bcattle Oct 01 '14 at 03:14
  • No, constraints are not created automatically. Here's a link to the Apple documentation on how to create them programmatically... https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutinCode/AutoLayoutinCode.html – Brian Shamblen Oct 01 '14 at 14:46

1 Answers1

0

Turns out setting

myViewController.automaticallyAdjustsScrollViewInsets = NO;

fixed this problem. Note that this property is set on the view controller, not the scroll view. Apparently the View Controller is attempting to account for the status bar by automatically adding 20 points of top content inset.

What's odd about this is that my view controller is a regular UIViewController not a UIScrollViewController. So somehow the view controller is detecting that it contains a UIScrollView and serriptitiously changing the content insets. And this behavior is on by default.

For more info see the docs and this SO answer also gives a lot of detail. It's also talked about in the iOS7 UI transition guide.

Community
  • 1
  • 1
bcattle
  • 12,115
  • 6
  • 62
  • 82