From 28b17896cc65a20f90f0279bca04f0bb14c5337a Mon Sep 17 00:00:00 2001 From: Sean Conrad Date: Tue, 25 Nov 2014 15:08:37 -0800 Subject: [PATCH 1/2] scrollToBottomAnimated: fix long message handling fixes #480 --- .../Controllers/JSQMessagesViewController.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSQMessagesViewController/Controllers/JSQMessagesViewController.m b/JSQMessagesViewController/Controllers/JSQMessagesViewController.m index 5ae0856b6..9cbbc9e25 100644 --- a/JSQMessagesViewController/Controllers/JSQMessagesViewController.m +++ b/JSQMessagesViewController/Controllers/JSQMessagesViewController.m @@ -366,7 +366,7 @@ - (void)scrollToBottomAnimated:(BOOL)animated } [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:items - 1 inSection:0] - atScrollPosition:UICollectionViewScrollPositionTop + atScrollPosition:UICollectionViewScrollPositionBottom animated:animated]; } From 6a04d7585d6920ba87287d29870e0a39cd24f089 Mon Sep 17 00:00:00 2001 From: Sean Conrad Date: Tue, 25 Nov 2014 16:02:44 -0800 Subject: [PATCH 2/2] improve layout for smaller cells in some cases, `Top` alignment is best - this commit handles those cases --- .../Controllers/JSQMessagesViewController.m | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/JSQMessagesViewController/Controllers/JSQMessagesViewController.m b/JSQMessagesViewController/Controllers/JSQMessagesViewController.m index 9cbbc9e25..c2540b437 100644 --- a/JSQMessagesViewController/Controllers/JSQMessagesViewController.m +++ b/JSQMessagesViewController/Controllers/JSQMessagesViewController.m @@ -365,8 +365,20 @@ - (void)scrollToBottomAnimated:(BOOL)animated return; } + // if last object is huge, use UICollectionViewScrollPositionBottom for better appearance + // else use Top + NSInteger finalRow = MAX(0, [self.collectionView numberOfItemsInSection:0] - 1); + NSIndexPath *finalIndexPath = [NSIndexPath indexPathForItem:finalRow + inSection:0]; + CGSize finalCellSize = [self.collectionView.collectionViewLayout sizeForItemAtIndexPath:finalIndexPath]; + + CGFloat maxHeight = CGRectGetHeight(self.collectionView.bounds) - self.collectionView.contentInset.top - CGRectGetHeight(self.inputToolbar.bounds); + + BOOL hugeCell = (maxHeight < finalCellSize.height); + UICollectionViewScrollPosition position = (hugeCell) ? UICollectionViewScrollPositionBottom : UICollectionViewScrollPositionTop; + [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:items - 1 inSection:0] - atScrollPosition:UICollectionViewScrollPositionBottom + atScrollPosition:position animated:animated]; }