Gorib Developer
Gorib Developer

Reputation: 597

How to set focus on collectionView in swift 4?

Hello I have a UICollectionView. In a particular cell I have many views like UserProfileView, ProductView, CommentBoxView etc. In CommentBoxView there is a text view where the user can type and then post the comment. After posting the comment I want to focus that particular cell, from which cell user have posted the comment. For this I have used :

func indexPathForPreferredFocusedView(in collectionView: UICollectionView) -> IndexPath? {
  return IndexPath(row: 2, section: 0)
}

I am testing with the hardcoded value but still it is not coming. Please any one help me regarding this matter.

Upvotes: 2

Views: 5654

Answers (2)

shiju86.v
shiju86.v

Reputation: 667

Try to use:

self.collectionView.scrollToItem(at:IndexPath(item: indexNumber, section: sectionNumber), at: .right, animated: false)

Upvotes: 3

woodcutting
woodcutting

Reputation: 271

Perhaps by "focus" you mean "scroll to." If you'd like to scroll to a cell, you should invoke

collectionView.scrollToItem(at: indexPath, scrollPosition: .centeredVertically, animated: true)

It may be useful to check out the documentation for this function.

As far as I know, while focus is a central concept in tvOS development, in iOS, its main function is to drive accessibility experiences. As such, I don't think it will have the effect you're expecting.

Upvotes: 3

Related Questions