From 5cc0fcd1d77d6296f57ce1c298301b9881cb4d4a Mon Sep 17 00:00:00 2001 From: David Yamnitsky Date: Wed, 1 Feb 2017 12:11:40 -0800 Subject: [PATCH] Fix calculation of vertical and horizontal center in scrollToObject Summary: Fix bug where scroll position would be incorrect in call to `-[IGListAdapter scrollToObject:supplementaryKinds:scrollDirection:scrollPosition:animated:` with scrollDirection/scrollPosition of UICollectionViewScrollDirectionVertical/UICollectionViewScrollPositionCenteredVertically or UICollectionViewScrollDirectionHorizontal/UICollectionViewScrollPositionCenteredHorizontally and with a collection view with nonzero contentInset. Reviewed By: jessesquires Differential Revision: D4475043 fbshipit-source-id: 4714ab0ed02e52e2d7f333624cc17858058ee57f --- CHANGELOG.md | 2 ++ Source/IGListAdapter.m | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70f4e1b5..0a3bbf69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,8 @@ This release closes the [2.2.0 milestone](https://github.com/Instagram/IGListKit - Fix potential bug with suppressing animations (by passing `NO`) during `-[IGListAdapter performUpdatesAnimated: completion:]` where user would see UI glitches/flashing. [Jesse Squires](https://github.com/jessesquires) [(tbd)]() +- Fix bug where scroll position would be incorrect in call to `-[IGListAdapter scrollToObject:supplementaryKinds:scrollDirection:scrollPosition:animated:` with scrollDirection/scrollPosition of UICollectionViewScrollDirectionVertical/UICollectionViewScrollPositionCenteredVertically or UICollectionViewScrollDirectionHorizontal/UICollectionViewScrollPositionCenteredHorizontally and with a collection view with nonzero contentInset. [David Yamnitsky](https://github.com/nitsky) + 2.1.0 ----- diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index 0e121e68..0afdb5e1 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -206,9 +206,11 @@ case UICollectionViewScrollPositionRight: contentOffset.x = offsetMax - collectionViewWidth - contentInset.left; break; - case UICollectionViewScrollPositionCenteredHorizontally: - contentOffset.x = offsetMid - collectionViewWidth / 2.0 - contentInset.left; + case UICollectionViewScrollPositionCenteredHorizontally: { + const CGFloat insets = (contentInset.left - contentInset.right) / 2.0; + contentOffset.x = offsetMid - collectionViewWidth / 2.0 - insets; break; + } case UICollectionViewScrollPositionLeft: case UICollectionViewScrollPositionNone: case UICollectionViewScrollPositionTop: @@ -223,9 +225,11 @@ case UICollectionViewScrollPositionBottom: contentOffset.y = offsetMax - collectionViewHeight - contentInset.top; break; - case UICollectionViewScrollPositionCenteredVertically: - contentOffset.y = offsetMid - collectionViewHeight / 2.0 - contentInset.top; + case UICollectionViewScrollPositionCenteredVertically: { + const CGFloat insets = (contentInset.top - contentInset.bottom) / 2.0; + contentOffset.y = offsetMid - collectionViewHeight / 2.0 - insets; break; + } case UICollectionViewScrollPositionTop: case UICollectionViewScrollPositionNone: case UICollectionViewScrollPositionLeft: