UICollectionView 没有滚动到 indexPath [英] UICollectionView isn't scrolling to indexPath
问题描述
我有一个 UICollectionView
,我将 translatesAutoresizingMaskIntoConstraints
设置为 NO
,并添加了一些 constraints
.当我尝试将其滚动到 indexPath
:
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];
然后它完全忽略它,并且不滚动.但是当我删除 translatesAutoresizingMaskIntoConstraints
时,它会滚动,但会忽略约束.
Then it completely ignores that, and doesn't scroll. But when I remove translatesAutoresizingMaskIntoConstraints
, then it scrolls, but the constraints are ignored.
我的问题是,当 translatesAutoresizingMaskIntoConstraints
设置为 NO
时,如何让 collectionView
滚动到 indexPath
代码>?
My question is, How can I get the collectionView
to scroll to an indexPath
when translatesAutoresizingMaskIntoConstraints
is set to NO
?
推荐答案
看了你的代码,发现了问题所在.我不确定为什么会这样,但是在应用约束后集合视图的 contentSize
为零.添加对 layoutIfNeeded
的调用到 setSelectedDate:
或 setDates:
修复了问题,
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;
}
这篇关于UICollectionView 没有滚动到 indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!