0

I am using collection view to showing the taken images of my app.I want as soon as the image taken the collection view should automatically move to last object in my collection view .My collection scrolls horizontally Please help me to do this.

I have searched in google and i have found i have applied this code in my project

i have applied this code in my project in viewwillappear ,it showing error, Please help me to do this

click here to see i am taking photo and showing in below ,ok ,in this image it has 3pictures ,if i again take a picture 4th picture will be there in collection view ,if user want to se the 4th picture means ,user want scroll by manually and can see it...

Here is my code:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [session startRunning];
    NSInteger section = [self numberOfSectionsInCollectionView:self.collection_View] - 1;
    NSInteger item = [self collectionView:self.collection_View numberOfItemsInSection:section] - 1;
    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
}

I want automatically scroll to the last pictures in horizontally Thanks in Advance !!!

Community
  • 1
  • 1
Saraswathi
  • 5
  • 1
  • 5

2 Answers2

2

Add the following line inside your viewWillAppear method code:

[collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];

Use this:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [session startRunning];
    NSInteger section = [self numberOfSectionsInCollectionView:self.collection_View] - 1;
    NSInteger item = [self collectionView:self.collection_View numberOfItemsInSection:section] - 1;
    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];

    [collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
}
vaibhav
  • 4,038
  • 1
  • 21
  • 51
  • [self.collection_View scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES]; – Saraswathi Oct 12 '16 at 09:43
  • if i use this ,[self.collection_View scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES]; it is showing error like Thread1:EXC_BAD_ACESS .i want to scroll horizontally only , i have tested with horizontal option also,same error only coming – Saraswathi Oct 12 '16 at 09:45
  • then it will have some more method like `UICollectionViewScrollPositionBottom`, `UICollectionViewScrollPositionUp` and some more just check, bad access error occur because not to implement `collectionView` properly check this also. – vaibhav Oct 12 '16 at 09:50
  • Thanks ,vaibhav ,what i did was ,it should not in view will appear ,i should be in after reloading the collection view ,because at first my collection view will be empty , – Saraswathi Oct 12 '16 at 10:07
  • ohh i see, anyway finally you got :) – vaibhav Oct 12 '16 at 10:10
0

Here's the Swift 4.2 version of the accepted answer

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let section = numberOfSections(in: collectionView) - 1
    let item = collectionView.numberOfItems(inSection: section) - 1
    let lastIndexPath = IndexPath(item: item, section: section)
    collectionView.scrollToItem(at: lastIndexPath, at: .bottom, animated: true)
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256