Iglistkit: Scroll to Bottom When Keyboard Toggled Causes Crash

Created on 8 Oct 2017  路  10Comments  路  Source: Instagram/IGListKit

Well, I have successfully implemented IGLisrKit. Only thing is that I had this function where it scrolls to the bottom of the content when the keyboard is toggled.

Here is my code for that method:

 @objc func handleKeyboardNotification(notification: NSNotification){
        if let userinfo = notification.userInfo{

            let keyboardFrame = (userinfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
            self.bottomConstraint?.constant = -(keyboardFrame.height)

            let isKeyboardShowing = notification.name == NSNotification.Name.UIKeyboardWillShow
            self.bottomConstraint?.constant = isKeyboardShowing ? -(keyboardFrame.height) : 0

            UIView.animate(withDuration: 0, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
                self.view.layoutIfNeeded()
            }, completion: { (completion) in
                if self.comments.count > 0  && isKeyboardShowing {
                    let indexPath = IndexPath(item: self.comments.count-1, section: 0)
                    self.collectionView.scrollToItem(at: indexPath, at: .top, animated: true)
                }
            })
        }
    }

It seems to be causing this error
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: {length = 2, path = 0 - 3}'

Any insight on how to fix this using iglist kit. I beleive it has to do with a count of the elements in the controller because that is what allowed me to do utilize it in my pre IGListKit Implementation

question

Most helpful comment

let indexPath = IndexPath(item: self.comments.count-1, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .top, animated: true)

You need to be sure that indexPath is valid before scrolling. You could use something like this when you want to scroll to the bottom of the UICollectionView: https://github.com/rnystrom/GitHawk/blob/ca233abc780fc59260c468a14b283bfbefc67e64/Classes/Views/UIScrollView%2BScrollToBottom.swift

Otherwise, the ListAdapter should have a scroll method so you can scroll to a specific section controller: https://instagram.github.io/IGListKit/Classes/IGListAdapter.html#/c:objc(cs)IGListAdapter(im)scrollToObject:supplementaryKinds:scrollDirection:scrollPosition:animated:

You could grab the latest object in your comments use the adapter API to get the section controller for a specific object and go from there.

All 10 comments

Can you repro this in an example app we can debug? There isn鈥檛 enough information for us to action yet.

You might also want to use the scrolling APIs on IGListAdapter instead.

Sent with GitHawk

Also try doing the debug dump that is in the issue template.

Sent with GitHawk

let indexPath = IndexPath(item: self.comments.count-1, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .top, animated: true)

You need to be sure that indexPath is valid before scrolling. You could use something like this when you want to scroll to the bottom of the UICollectionView: https://github.com/rnystrom/GitHawk/blob/ca233abc780fc59260c468a14b283bfbefc67e64/Classes/Views/UIScrollView%2BScrollToBottom.swift

Otherwise, the ListAdapter should have a scroll method so you can scroll to a specific section controller: https://instagram.github.io/IGListKit/Classes/IGListAdapter.html#/c:objc(cs)IGListAdapter(im)scrollToObject:supplementaryKinds:scrollDirection:scrollPosition:animated:

You could grab the latest object in your comments use the adapter API to get the section controller for a specific object and go from there.

Okay i found the scroll(toObject) method thanks ill look into it

listAdapter.scroll(to: <#T##Any#>, supplementaryKinds: <#T##[String]?#>, scrollDirection: <#T##UICollectionViewScrollDirection#>, scrollPosition: <#T##UICollectionViewScrollPosition#>, animated: <#T##Bool#>)

So how would this method trigger on keyboard toggle when the current object is in the listAdapter datasource function

@rnystrom @weyert

That's an implementation detail on your end?

Um well, the thing I copy and paste is just the function that comes when you attempt to use that function. Yeah, Im just trying to figure out where this function would fit in at.

Based on the function I had to accomplish goal to scroll to the bottom

Closing old issues that seem to be resolved or are inactive. 馃槃

If this isn't resolved and you still need/want help, please re-open! 馃槉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lucabartoletti picture lucabartoletti  路  3Comments

kleiberjp picture kleiberjp  路  3Comments

jessesquires picture jessesquires  路  3Comments

joseph-francis picture joseph-francis  路  3Comments

alexwillrock picture alexwillrock  路  3Comments