问题
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