Skip to content
This repository has been archived by the owner on Aug 14, 2019. It is now read-only.

Commit

Permalink
Added method to scroll to a specific row, optionally animated.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianludwig committed Feb 9, 2016
1 parent 90471f3 commit baa33d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@
*/
- (void)scrollToBottomAnimated:(BOOL)animated;

/**
* Scrolls the collection view such that the cell in the given row is completely visible, above the `inputToolbar`.
*
* @param indexPath The indexPath that will be visible
* @param animated Pass `YES` if you want to animate scrolling, `NO` if it should be immediate.
*/
- (void)scrollToIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

/**
Call to super required.
*/
Expand Down
30 changes: 20 additions & 10 deletions JSQMessagesViewController/Controllers/JSQMessagesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,20 @@ - (void)scrollToBottomAnimated:(BOOL)animated
return;
}

NSInteger items = [self.collectionView numberOfItemsInSection:0];
NSIndexPath *lastCell = [NSIndexPath indexPathForItem:([self.collectionView numberOfItemsInSection:0] - 1) inSection:0];
[self scrollToIndexPath:lastCell animated:animated];
}


if (items == 0) {
- (void)scrollToIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{
if ([self.collectionView numberOfSections] <= indexPath.section) {
return;
}

NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:indexPath.section];

if (numberOfItems == 0) {
return;
}

Expand All @@ -366,19 +377,18 @@ - (void)scrollToBottomAnimated:(BOOL)animated
animated:animated];
return;
}

NSInteger item = MAX(MIN(indexPath.item, numberOfItems - 1), 0);
indexPath = [NSIndexPath indexPathForItem:item inSection:0];

// workaround for really long messages not scrolling
// if last message is too long, use scroll position bottom for better appearance, else use top
// possibly a UIKit bug, see #480 on GitHub
NSUInteger finalRow = MAX(0, [self.collectionView numberOfItemsInSection:0] - 1);
NSIndexPath *finalIndexPath = [NSIndexPath indexPathForItem:finalRow inSection:0];
CGSize finalCellSize = [self.collectionView.collectionViewLayout sizeForItemAtIndexPath:finalIndexPath];

CGSize cellSize = [self.collectionView.collectionViewLayout sizeForItemAtIndexPath:indexPath];
CGFloat maxHeightForVisibleMessage = CGRectGetHeight(self.collectionView.bounds) - self.collectionView.contentInset.top - CGRectGetHeight(self.inputToolbar.bounds);

UICollectionViewScrollPosition scrollPosition = (finalCellSize.height > maxHeightForVisibleMessage) ? UICollectionViewScrollPositionBottom : UICollectionViewScrollPositionTop;

[self.collectionView scrollToItemAtIndexPath:finalIndexPath
UICollectionViewScrollPosition scrollPosition = (cellSize.height > maxHeightForVisibleMessage) ? UICollectionViewScrollPositionBottom : UICollectionViewScrollPositionTop;

[self.collectionView scrollToItemAtIndexPath:indexPath
atScrollPosition:scrollPosition
animated:animated];
}
Expand Down

0 comments on commit baa33d2

Please sign in to comment.