From 74e03aad01cf65a3561719265f1c123761811238 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 11 Jan 2018 12:26:15 -0500 Subject: [PATCH] Fix intermittent content offset problem Using the CollectionView's frame to determine if we're at the bottom doesn't make sense unless the collection view is correctly layed out. // FREEBIE --- .../ConversationView/ConversationInputToolbar.h | 2 -- .../ConversationView/ConversationViewController.m | 11 ++++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.h b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.h index 3f7ee27b8..5b7df0499 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.h +++ b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.h @@ -26,8 +26,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)didApproveAttachment:(SignalAttachment *)attachment; -- (void)toolbarHeightDidChange:(CGFloat)newHeight; - @end #pragma mark - diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 4e9aa5d53..0a5103725 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -3045,7 +3045,9 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { // content to fill the collection view at its current size. CGFloat contentOffsetYBottom = MAX(0.f, contentHeight + self.collectionView.contentInset.bottom - self.collectionView.bounds.size.height); - BOOL isScrolledToBottom = (self.collectionView.contentOffset.y > contentOffsetYBottom - kIsAtBottomTolerancePts); + + CGFloat distanceFromBottom = contentOffsetYBottom - self.collectionView.contentOffset.y; + BOOL isScrolledToBottom = distanceFromBottom <= kIsAtBottomTolerancePts; return isScrolledToBottom; } @@ -3693,7 +3695,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { CGPoint newOffset = CGPointMake(0, newYOffset); // If the user is dismissing the keyboard via interactive scrolling, any additional conset offset feels - // redundant, so we only adjust content offset when *presenting* the keyboard. + // redundant, so we only adjust content offset when *presenting* the keyboard (i.e. when insetChange > 0). if (insetChange > 0 && newYOffset > keyboardEndFrame.origin.y) { [self.collectionView setContentOffset:newOffset animated:NO]; } @@ -3747,10 +3749,13 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { - (void)scrollToBottomAnimated:(BOOL)animated { OWSAssert([NSThread isMainThread]); - if (self.isUserScrolling) { return; } + + // Ensure the view is fully layed out before we try to scroll to the bottom, since + // we use the collectionView bounds to determine where the "bottom" is. + [self.view layoutIfNeeded]; CGFloat contentHeight = self.safeContentHeight;