Focusing the initial item in a collection view in Catalyst on macOS

Viewed 150

I have a Catalyst app running on macOS where I need to select and focus the first item in a collection view. By default a programmatically-selected item is only selected, not focused. To demonstrate what I mean, here is the app on launch...

enter image description here

And here is the app after I manually select the first item using the mouse or keyboard...

enter image description here

From what I understand, Catalyst uses the Focus Engine from tvOS to manage focus, and therefore applying the same concepts here should achieve the same result. However, I've had zero luck getting this working.

Things I have tried...

  • Overriding preferredFocusEnvironments on the view controller and returning the collection view, and implementing indexPathForPreferredFocusedView(in:) in the collection view delegate. This works in so much as both methods are called, but has no effect on the outcome.
  • Overriding preferredFocusEnvironments and returning the currently selected cell from the collection view.
  • Overriding preferredFocusEnvironments and returning the cell at a hardcoded IndexPath(item: 0, section: 0), as per this Big Nerd Ranch article.

Unfortunately, none of these approaches has the desired outcome of actually focusing the selected item. Can anyone advise on how to achieve this?

1 Answers
Related