I'd like to make a UICollectionView do an animated scroll to a specific item.
This works most of the time, but occasionally the item that I'm trying to scroll to doesn't end up being shown.
- (void)onClick {
// (Possibly recompute the _items array.)
NSInteger target_idx = // (...some valid index of _items)
NSIndexPath *item_idx = [NSIndexPath indexPathForItem:target_idx inSection:0];
[self scrollToItem:item_idx];
}
- (void)scrollToItem:(NSIndexPath*)item_idx {
// Make sure our view is up-to-date with the data we want to show.
NSInteger num_items = [self.collection_view numberOfItemsInSection:0];
if (num_items != _items.count) {
[self.collection_view reloadData];
}
[self.collection_view
scrollToItemAtIndexPath:item_idx
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
}
self.collection_view
is a UICollectionView that consists of a single row of items, with a standard flow layout and horizontal scrolling enabled.reloadData
before scrolling, because it's possible that _items
has changed since the UICollectionView last displayed it.animated:NO
then everything works as expected.indexPathsForVisibleItems
reveals that the UICollectionView doesn't think that the target item is visible.Any ideas why scrolling to an item silently fails sometimes?
Update: the problem seems to come from reloading and scrolling in quick succession; if there is no reload, scrolling behaves as expected. Are there any idioms for scrolling to an item after loading new data?
I've just run in to a similar / the same issue after adding an item to a UICollectionView
.
The issue seems to be that immediately following [collectionView reloadData]
or [collectionView insertItemsAtIndexPaths: @[newItemIndexPath]]
, the collection view's content size is not yet updated.
If you then try to scroll the added item visible, it will fail because the content rect doesn't yet include space for the new item.
There is a simple and fairly robust work around, which is to post the scroll event on to the next iteration of the run loop like this:
const NSUInteger newIndex =
[self collectionView: self.collectionView numberOfItemsInSection: 0] - 1;
NSIndexPath *const newPath =
[NSIndexPath indexPathForItem: newIndex
inSection: 0];
[self.collectionView insertItemsAtIndexPaths: @[newPath]];
UICollectionViewLayoutAttributes *const layoutAttributes =
[self.collectionView layoutAttributesForItemAtIndexPath: newPath];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView scrollRectToVisible: layoutAttributes.frame
animated: YES];
});
While it works, this "post the scroll call on the next run loop tick" shenanigans feels hacky.
It would be preferable if UICollectionView
could invoke a callback when it finished updating the content rect. UIKit has callbacks of this style for other methods that perform asynchronous updates to its model. For example, a completion block for UIViewController
transitions and UIView
animations.
UICollectionView
does not provide this callback. As far as I know, there is no other simple clean way to find when it completes its update. In the absence of this, the next run loop tick is a viable horse proxy for the callback unicorn we would prefer to use.
It's probably useful to know that UITableView
also has this issue. A similar workaround should work there too.
With some help from @NicholasHart, I think I understand the problem.
Trying to reloadData
and then performing an animated scroll to a new position makes sense (and seems to work) as long as the reload makes the collection bigger.
When reloading shrinks the collection, however, the starting point for an animated scroll might not exist anymore. This makes animation troublesome.
For example, if you start with the view scrolled all the way to the right (so that your rightmost item is visible) and then reload and lose half of your items, it's not clear what the starting point for the animation should be. Trying to do an animated scroll in this situation results in either a no-op or a jump to a very strange position.
One solution that looks reasonably good is to only animate if the collection is getting bigger:
- (void)scrollToItem:(NSIndexPath*)item_idx {
// Make sure our view is up-to-date with the data we want to show.
NSInteger old_num_items = [self.collection_view numberOfItemsInSection:0];
NSInteger new_num_items = _items.count;
if (old_num_items != new_num_items) {
[self.collection_view reloadData];
}
// Animating if we're getting smaller doesn't really make sense, and doesn't
// seem to be supported.
BOOL is_expanding = new_num_items >= old_num_items;
[self.collection_view
scrollToItemAtIndexPath:item_idx
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:is_expanding];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With