美文网首页iOS遇到的问题
UICollectionView滚动到指定Cell不生效问题

UICollectionView滚动到指定Cell不生效问题

作者: 名字不重要呀 | 来源:发表于2020-06-02 21:32 被阅读0次

问题描述:

在页面将要出现的时候,调用scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animatedUICollectionView滚动到指定的cell上不生效

解决:

scrollToItemAtIndexPath:之前先调用

[self.collectionView layoutIfNeeded];
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];

然后就可以正常滚动到指定的cell了.

这里[self.collectionView layoutIfNeeded];如果换成[self layoutIfNeeded];并不能正常的滚动到指定的位置

相关文章

网友评论

    本文标题:UICollectionView滚动到指定Cell不生效问题

    本文链接:https://www.haomeiwen.com/subject/txwsvhtx.html