How to disable The automatic focus change in tvOS Apple TV

Viewed 715

I Have a details page with collectionView. The collection view has a header view which has some buttons(play buttons). When the page first opens the focus will be on the play button. after loading the header view we are inserting rail cells for showing related items in the collectionView. But whenever the rails get loaded the focus automatically moves to the rail cell. There is no focus handling done for collectionView only one is this code

func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
        return true
    }

how can I remove this behavior? I tried "should update focus" to get this focus change and block it

override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
        <#code#>
    }

but this function is getting triggered only when focus change is through remote and no automatic focus change (like my case) Now am handling the situation with

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        if let headerView = getHeaderView(), let collectionViewCell = detailsCollectionView.cellForItem(at: IndexPath(item: 0, section: 0)) as? CollectionViewCell {
            let previouslyFocusedView = context.previouslyFocusedView
            let nextFocusedView = context.nextFocusedView
            if previouslyFocusedView?.isDescendant(of: headerView.playButton) ?? false, nextFocusedView?.isDescendant(of: collectionViewCell.getInnerCollectionView()) ?? false {
                cancelFocusChange(previouslyFocusedView: previouslyFocusedView)
            } 
        }
        super.didUpdateFocus(in: context, with: coordinator)
    }

but I don't think it is a good idea. There must be a reason why this focus change is happening. If anyone knows the answer to this, please do help.

0 Answers
Related