Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView scroll to last item without Animation

I have a UICollectionView to display chat messages. At the beginning I load the existing messages to the collectionView and scroll the collectionView down to the last message with this method:

- (void)scrollToLastMessageAnimated:(BOOL)animated;
{
    if (_messages.count == 0) { return; }

    NSUInteger indexOfLastSection = _messagesBySections.count - 1;
    NSInteger indexOfMessageInLastSection = [_messagesBySections[indexOfLastSection] count] - 1;
    NSIndexPath *path = [NSIndexPath indexPathForItem:indexOfMessageInLastSection
                                            inSection:indexOfLastSection];

    [_collectionView scrollToItemAtIndexPath:path
                           atScrollPosition:UICollectionViewScrollPositionCenteredVertically
                                   animated:animated];
}

This only works animated when I call it in the viewDidAppear: method and not in viewWillAppear: method. How can I scroll down without animation?

like image 777
electronix384128 Avatar asked Jun 02 '14 15:06

electronix384128


People also ask

What is the use of scrollto method in collectionview?

CollectionView defines two ScrollTo methods, that scroll items into view. One of the overloads scrolls the item at the specified index into view, while the other scrolls the specified item into view.

What is the scrollbarvisibility enumeration in collectionview?

CollectionView defines HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties, which are backed by bindable properties. These properties get or set a ScrollBarVisibility enumeration value that represents when the horizontal, or vertical, scroll bar is visible. The ScrollBarVisibility enumeration defines the following members:

What are the properties of scrolltorequested event object?

The ScrollToRequestedEventArgs object that accompanies the ScrollToRequested event has many properties, including IsAnimated, Index, Item, and ScrollToPosition. These properties are set from the arguments specified in the ScrollTo method calls.

What is the default Scroll bar behavior?

Default indicates the default scroll bar behavior for the platform, and is the default value for the HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties. Always indicates that scroll bars will be visible, even when the content fits in the view.


3 Answers

Here it is...

NSInteger section = [self.collectionView numberOfSections] - 1;
NSInteger item = [self.collectionView numberOfItemsInSection:section] - 1;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionBottom) animated:YES];
like image 190
itsji10dra Avatar answered Oct 25 '22 11:10

itsji10dra


I also provided my answer in another question of yours but I am also writing it here, since this answer is being related only with this problem

Solution

To scroll the view at the last index without crashing before view appears, you first need to trigger the reload of your collectionView data. After it has been reloaded call your method to scroll your view.

-(void)viewWillAppear:(BOOL)animated {
    [collectionView reloadData];
    [self scrollToLastMessageAnimated:YES];
}

Update

[collectionView setContentOffset:CGPointMake(0, CGFLOAT_MAX)]
like image 34
E-Riddie Avatar answered Oct 25 '22 12:10

E-Riddie


Swift 3.0 Code :

let index = IndexPath(item: (self.mFetchedResultsControllerVar?.fetchedObjects?.count)! - 1, section: 0)
self.collectionView?.scrollToItem(at: index, at: UICollectionViewScrollPosition.bottom, animated: true)
like image 41
krish Avatar answered Oct 25 '22 13:10

krish