UICollectionView isn't scrolling to indexPath

随声附和 提交于 2019-12-25 03:12:18

问题


I have a UICollectionView that I set the translatesAutoresizingMaskIntoConstraints to NO, and added some constraints. When I try scrolling it to an indexPath:

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

Then it completely ignores that, and doesn't scroll. But when I remove translatesAutoresizingMaskIntoConstraints, then it scrolls, but the constraints are ignored.

My question is, How can I get the collectionView to scroll to an indexPath when translatesAutoresizingMaskIntoConstraints is set to NO?


回答1:


After looking at your code, I found the problem. I'm not sure why this is the case, but the contentSize of the collection view was zero after applying the constraints. Adding a call to layoutIfNeeded to either setSelectedDate: or setDates: fixed the problem,

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

    self.selectedDate = nil;
}


来源:https://stackoverflow.com/questions/30655351/uicollectionview-isnt-scrolling-to-indexpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!