UICollectionView 没有滚动到 indexPath

UICollectionView isn't scrolling to indexPath

我有一个 UICollectionView,我将 translatesAutoresizingMaskIntoConstraints 设置为 NO,并添加了一些 constraints。当我尝试将其滚动到 indexPath:

[self.datesCollectionView selectItemAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];

然后它完全忽略它,并且不滚动。但是当我删除 translatesAutoresizingMaskIntoConstraints 时,它会滚动,但约束会被忽略。

我的问题是,当 translatesAutoresizingMaskIntoConstraints 设置为 NO 时,如何让 collectionView 滚动到 indexPath

看了你的代码,我发现了问题。我不确定为什么会这样,但是在应用约束后集合视图的 contentSize 为零。将对 layoutIfNeeded 的调用添加到 setSelectedDate:setDates: 解决了问题,

- (void)setDates:(NSArray *)dates {
    _dates = dates;
    [self.datesCollectionView layoutIfNeeded];
    [self.datesCollectionView reloadData];

    self.selectedDate = nil;
}