iOS 14 scrollToItemAtIndexPath 方法失效的解决方案

4,834 阅读1分钟

App升级到iOS14后,项目中遇到UICollectionView 横向滚动时,UICollectionViewCell 不再居中,经测试在iOS14系之前的系统,是没有此问题的,最后定位是以下方法在iOS14系统失效了

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated

直接上解决方案:

 UICollectionViewLayoutAttributes *attri = [self.flowLayout layoutAttributesForItemAtIndexPath:indexPath];
 CGPoint point = CGPointMake(attri.frame.origin.x - ([UIScreen mainScreen].bounds.size.width - self.flowLayout.itemWidth)/2, attri.frame.origin.y);
 [self.collectionView setContentOffset:point animated:animated];

系统方法,不好究其原因,希望同样遇到此问题的小伙伴避免采坑。