How maintain scroll position? #441
Comments
Hi, @educosta! Before loading a new bunch of messages save your current y-offset (but counting from the bottom, not from the top). I.e. CGFloat oldOffset = self.collectionView.contentSize.height - self.collectionView.contentOffset.y After that, load new messages to dataSource, call self.collectionView.contentOffset = CGPointMake(0.0, self.collectionView.contentSize.height - oldOffset); |
thanks for the answer @dim0v! |
Hi @dim0v Works for you? I tried on demo project but not works :( |
Here is the code I am using in my current project (a bit simplified and cleared from unrelated things). It works fine. - (void) loadHistory {
CGFloat oldOffsetReversed = self.collectionView.contentSize.height - self.collectionView.contentOffset.y;
NSUInteger pageSize = [self fetchNextPage]; //reloadData is called inside
[self.view layoutIfNeeded];
CGFloat offset = self.collectionView.contentSize.height - oldOffsetReversed;
self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x, offset);
} |
Also, if message loading is asynchronous, this should be done inside the completion handler. If still doesn't work for you, please, post your code somwhere, i'll try to figure out why :) Btw, I assumed you are loading messages on the top. If new messages are added to the bottom, then setting automaticallyScrollsToMostRecentMessage to NO should be enough to preserve scroll position. |
Hi @dim0v Now works, I missing [self.view layoutIfNeeded]; after reload You're the best! |
@dim0v works perfectly even on iOS 9 ;) |
I have this issue too and I've tried the layoutIfNeeded. But with a lot of cells and self sizing, it completely freeze the UI. |
How can I still scroll position when I load new messages?
If disabled automaticallyScrollsToMostRecentMessage scroll to up, if enabled scroll to down.
Thank you
The text was updated successfully, but these errors were encountered: