-->

UICollectionView doesn't scroll after setting

2019-07-16 10:10发布

问题:

I have a collection view that scrolls horizontally and spans its parent view's full width. My cheap way to achieve paging on it is to set the cell widths to be equal to 1/3 of the collection view width, and to set that same amount of width as left and right content insets.

I disable scrolling in IB and replace with left and right swipe recognizers. My code almost works without setting contentInset, but setting the contentInset seems prevent any scrolling from happening

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGFloat itemWidth = self.collectionView.bounds.size.width/3.0;
    NSInteger count = [self collectionView:self.collectionView numberOfItemsInSection:0];
    self.collectionView.contentSize = (CGSize){ .width=itemWidth*count, .height=self.collectionView.bounds.size.height };
    // uncomment this line, and the scroll code in the swipes below fails to work
    //self.collectionView.contentInset = UIEdgeInsetsMake(0, itemWidth, 0, itemWidth);
    self.collectionView.contentOffset = (CGPoint){ .x=self.collectionView.contentSize.width/2.0, .y=0 };
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = self.view.bounds.size.width/3.0;
    return (CGSize){ .width=width, .height=collectionView.bounds.size.height };
}

This code handles the swipes...

- (NSIndexPath *)centerIndexPath {
    CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
    CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
    return [self.collectionView indexPathForItemAtPoint:visiblePoint];
}

- (void)swipeLeft:(UISwipeGestureRecognizer *)gr {
    NSIndexPath *centerIndexPath = [self centerIndexPath];
    NSLog(@"at %@", centerIndexPath);

    if (centerIndexPath.row < [self collectionView:self.collectionView numberOfItemsInSection:0]-1) {
        [self.collectionView scrollToItemAtIndexPath:centerIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
}

- (void)swipeRight:(UISwipeGestureRecognizer *)gr {
    NSIndexPath *centerIndexPath = [self centerIndexPath];
    NSLog(@"at %@", centerIndexPath);

    if (centerIndexPath.row > 0) {
        [self.collectionView scrollToItemAtIndexPath:centerIndexPath atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
    }
}

All of this works, except when I set the contentInsets in the setup above. Then, even though I reach the scrollToItemAtIndexPath: code in the debugger, no scrolling occurs.

It's important to have those insets, because I want user to understand that center item is the selected item.

Can somebody explain why contentInset spoils scrolling and how to fix?

回答1:

It looks like UICollectionView has its own built-in way to handle insets:

https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW1

Using Section Insets to Tweak the Margins of Your Content Section insets are a way to adjust the space available for laying out cells. You can use insets to insert space after a section’s header view and before its footer view. You can also use insets to insert space around the sides of the content. Figure 3-5 demonstrates how insets affect some content in a vertically scrolling flow layout.

Figure 3-5 Section insets change the available space for laying out cells