tadejr / ResizingTokenField

A token field implementation for iOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Automatic Scroll

Ahmed-Masoud opened this issue · comments

I have my token field with fixed height is there a way to make it scroll automatically when new lines is added so the last thing inserted is the visible thing and thankns

The token field does have an internal collection view, but currently expects to grow as needed. You should try adding it to a parent scroll view, and limiting the scroll view's height. You can then scroll the parent scroll view to bottom if needed after adding tokens, or when token field invokes the resizingTokenFieldWill/DidChangeIntrinsicHeight callback.

since the collection view is private so this is how i worked around it I hope you can implement it in the pod or expose the collection view
I loop on the subviews to hold a refrence on the collection view which is the only subview
tokenField.subviews.forEach { (view) in if let collectionView = view as? UICollectionView { tagsCollectionView = collectionView } }
then after adding any token i scroll to the last element
tokenField.append(tokens: tags, animated: true) let lastItemIndex = NSIndexPath(item: tokenField.tokens.count + 1, section: 0) tagsCollectionView?.scrollToItem(at: lastItemIndex as IndexPath, at: .bottom, animated: true)

@Ahmed-Masoud , I have the same issue, while using your solution I am facing problem with "tagsCollectionView". can you help in this, where you did this and what is tagsCollectionView. I am rounding with this issue from past two day.
Thank you.

@NallapatiMalleswari
What I did in details is

  1. I initialized an optional UIVollectionView named tagsCollectionView
  2. In viewWillAppear I hold a reference to the internal collection view in the tokenField as so where token field is of type ResizingTokenField
tokenField.subviews.forEach { (view) in if let collectionView = view as? UICollectionView { tagsCollectionView = collectionView } }
  1. Whenever I want to add new item in the tokenField I add it like this
tokenField.append(tokens: tags, animated: true) 
let lastItemIndex = NSIndexPath(item: tokenField.tokens.count + 1, section: 0) 
tagsCollectionView?.scrollToItem(at: lastItemIndex as IndexPath, at: .bottom, animated: true)

@Ahmed-Masoud, works for me.