tvos How to always focus specific tableViewCell

1.7k Views Asked by At

I've creating tvOs app and had stucked with focus engine.

I have tableview, and set remembersLastFocusedIndexPath true. And I have to override indexPathForPreferredFocusedView. When I focus first time, indexPathForPreferredFocusedView get called, and I get the cell focused, what I need. But in other times I get last focused indexpath. When I define remembersLastFocusedIndexPath to false, indexPathForPreferredFocusedViewnot being called.

What I need to do, to focus only cells, what I need?

2

There are 2 best solutions below

2
On

Override the below function and return indexpath that you want to focus.

override func indexPathForPreferredFocusedView(in tableView: UITableView) -> IndexPath? {}

demo

0
On

indexPathForPreferredFocusedView is called only once when the view is about to get focus for the very first time or during reload.

If you are expecting to get some other Focus behaviour, you can try with preferredFocusEnvironments to get your focus behavior.

Also, it will focus to the last focused IndexPath because that is the default behavior in tvOS. The variable restoreFocusAfteViewTransition is set to true by default. You can change this to false in ViewController and check if you are able to achieve what you wanted.