Ios UICollectionView没有';设置contentInset后不能滚动

Ios UICollectionView没有';设置contentInset后不能滚动,ios,uicollectionview,uicollectionviewlayout,Ios,Uicollectionview,Uicollectionviewlayout,我有一个水平滚动的集合视图,它跨越其父视图的整个宽度。我实现分页的廉价方法是将单元格宽度设置为集合视图宽度的1/3,并将宽度设置为与左侧和右侧内容插入相同的大小 我禁用IB中的滚动,并替换为左右滑动识别器。我的代码几乎不需要设置contentInset,但设置contentInset似乎可以防止任何滚动 - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; CGFloat itemWidth = self.

我有一个水平滚动的集合视图,它跨越其父视图的整个宽度。我实现分页的廉价方法是将单元格宽度设置为集合视图宽度的1/3,并将宽度设置为与左侧和右侧内容插入相同的大小

我禁用IB中的滚动,并替换为左右滑动识别器。我的代码几乎不需要设置
contentInset
,但设置
contentInset
似乎可以防止任何滚动

- (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 };
}
此代码处理刷卡

- (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];
    }
}
-(nsindepath*)centerindepath{
CGRect visibleRect=(CGRect){.origin=self.collectionView.contentOffset,.size=self.collectionView.bounds.size};
CGPoint visiblePoint=CGPointMake(CGRectGetMidX(visibleRect),CGRectGetMidY(visibleRect));
返回[self.collectionView indexPathForItemAtPoint:visiblePoint];
}
-(无效)swipeLeft:(UISweepGestureRecognitor*)gr{
nsindepath*centerIndexPath=[self-centerIndexPath];
NSLog(@“at%@”,centerIndexPath);
if(centerIndexPath.row<[self-collectionView:self.collectionView numberOfItemsInSection:0]-1){
[self.collectionView scrollToItemAtIndexPath:centerIndexPath atScrollPosition:UICollectionViewScrollPositionLeft动画:是];
}
}
-(无效)swipeRight:(UISwipegestureRecognitor*)gr{
nsindepath*centerIndexPath=[self-centerIndexPath];
NSLog(@“at%@”,centerIndexPath);
如果(centerIndexPath.row>0){
[self.collectionView scrollToItemAtIndexPath:centerIndexPath atScrollPosition:UICollectionViewScrollPositionRight动画:是];
}
}
除了在上面的设置中设置ContentInset之外,所有这些都可以工作。然后,即使我在调试器中找到了scrollToItemAtIndexPath:代码,也不会发生滚动

有这些插图很重要,因为我希望用户理解中心项目是所选项目


有人能解释一下contentInset破坏滚动的原因以及如何修复吗?

看起来UICollectionView有自己的内置方式来处理插入:

使用节插入调整内容的边距 剖面插图是调整单元布局可用空间的一种方法。可以使用插入在节的页眉视图之后和页脚视图之前插入空格。也可以使用插图在内容的侧面周围插入空间。图3-5演示了插入如何影响垂直滚动流布局中的某些内容

图3-5剖面插图更改了用于布置单元的可用空间


使用剖面插图调整内容剖面插图的边距是调整单元格布局可用空间的一种方法。图3-5剖面插图更改了用于布置单元的可用空间。那行吗?见鬼,行!你想把它作为一个答案吗?