tvOS - Reset CollectionView's focus at one point of time with remembersLastFocusedIndexPath = true [swift]

785 Views Asked by At

I have a setup a collection view with remembersLastFocusedIndexPath set to true.

CollectionView is designed such a way that it has nested collection views,

CollectionView 
  -> Section 1
    --->Header
    --->Cell nested with collectionview 
  -> Section 2
    --->Header
    --->Cell nested with collectionview 
....so on....

There is pagination too.

Now if user scrolls to say 2nd item in 7th section and clicks menu in tv remote, I am scrolling collection to top. [either by setting content offset to 0 or scrolling to 0th index]

Requirement: After scrolling to 0th index, if user tries to focus on first cell in 0th section, I want focus to be at 0th index.

Issue: But now collection view remembers last focused index path and scrolls to 2nd item in 7th section.

Expected: I do not want collection view to remember the last focused index path in this case. In all other cases, it should be remembered. i.e, after scrolling to top, I want do something like

collectionView.scrollToTop()
resetFocus() //which should reset collection view's focus to initial index.


func resetFocus() { 
//What should be written here to reset the focus of collection view.
}
1

There are 1 best solutions below

0
On

try also implementing indexPathForPreferredFocusedView after you reset focus. Documentation here.