5

I have a UICollection view which looks like this (see two purple and blue borders):

UICollectionView with issue

On iOS 10 there is no header/whitespace, but on iOS11 there is. I've tried everything mentioned here: How can I enable/disable section headers in UICollectionView programmatically?

And my code currently looks like this:

- (void) viewDidLoad
{
    [super viewDidLoad];

    autocompleteLayout = [[SQLProAutocompleteFlowLayout alloc] init];
    autocompleteLayout.headerReferenceSize = CGSizeZero;

    autocompleteCollectionView = [[UICollectionView alloc] initWithFrame: CGRectZero
                                                    collectionViewLayout: autocompleteLayout];

    autocompleteCollectionView.insetsLayoutMarginsFromSafeArea = NO;

    [self.view addSubview: autocompleteCollectionView];

    // removed layout code (leftAnchor, rightAnchor, topAnchor, bottomAnchor).

    autocompleteCollectionView.delegate = self;
    autocompleteCollectionView.dataSource = self;

    // Removed nib register code

    self.view.layer.borderColor = [UIColor redColor].CGColor;
    self.view.layer.borderWidth = 1;

    autocompleteCollectionView.layer.borderColor = [UIColor blueColor].CGColor;
    autocompleteCollectionView.layer.borderWidth = 2;
}

- (CGSize) collectionView: (UICollectionView *) collectionView
                   layout: (UICollectionViewLayout *) collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeZero;
}

And my FlowLayout looks like this:

- (void) setupLayout
{
    self.minimumInteritemSpacing = 0;
    self.minimumLineSpacing      = 0;
    self.scrollDirection         = UICollectionViewScrollDirectionVertical;
} // End of setupLayout

- (void) prepareLayout
{
    [super setItemSize: [self itemSize]];
    [super setSectionInset: [self sectionInset]];
    self.minimumLineSpacing = 0;

    [super prepareLayout];
} // End of prepareLayout

- (CGFloat) itemWidth
{
    return self.collectionView.frame.size.width;
}

- (CGSize) itemSize
{
    CGSize result = CGSizeMake(self.collectionView.frame.size.width,
                               rowHeight);

    return result;
}

- (void) setItemSize:(CGSize)itemSize
{
    self.itemSize = CGSizeMake(self.collectionView.frame.size.width,
                               rowHeight);
}

- (CGPoint) targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
{
    return CGPointZero;
}

I have also verified that the UICollectionView's contentInset is zero'd out.

What am I missing? Why does my UICollectionView still have whitespace?

Kyle
  • 17,317
  • 32
  • 140
  • 246
  • try to disable viewcontroller's Adjust scrollview insets . or disable navigationbar's translucent property . [[UINavigationBar appearance] setTranslucent:NO]; – KKRocks Jul 28 '17 at 13:07
  • OP didn't say that it's in its own view controller. – NRitH Jul 28 '17 at 13:07
  • @KKRocks just tried both. No luck (its not in a navigation controller, but gave it a shot). NRitH -- it is contained in a UIViewController. – Kyle Jul 28 '17 at 13:11
  • try to set inset of collectionview [collectionView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)]; – KKRocks Jul 28 '17 at 13:16
  • @kkrocks thanks, but no difference. – Kyle Jul 28 '17 at 13:20
  • 1
    try this may help this also : [collectionView setContentInset:UIEdgeInsetsMake(-20, 0, 0, 0)]; – KKRocks Jul 28 '17 at 13:23
  • @KKRocks that does do it... but it also makes me feel dirty! – Kyle Jul 28 '17 at 13:24
  • yes but we cant help deeply without debug your code . so you can use this solution until you not getting proper solution. – KKRocks Jul 28 '17 at 13:26
  • @KKRocks appreciate the help! Thanks. – Kyle Jul 28 '17 at 13:27
  • or try this also - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(-20, 5, 5, 5); } – KKRocks Jul 28 '17 at 13:27
  • The delegate method does seem to make any difference. I'll stick with the -20 offset until I can figure out the underlying reasoning. Thanks again! – Kyle Jul 28 '17 at 13:38

2 Answers2

14

In iOS 11, there have been some changes to UIScrollView.

Try setting the contentInsetAdjustmentBehavior property

collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
wsidell
  • 712
  • 7
  • 14
  • 2
    If you are supporting iOS < 11: `if #available(iOS 11.0, *) { collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.never }` – vicegax Oct 13 '17 at 15:59
1

Try this:

 self.automaticallyAdjustsScrollViewInsets = false
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
        return 0;
    }

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}


- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
   return UIEdgeInsetsMake(-25,0,0,0);  // top, left, bottom, right
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Ramesh.G
  • 359
  • 1
  • 10