How to initially enable focus in a collection view programmatically?

Hi

I've set up a list UICollectionView with focus (without selectionFollowsFocus as I must distinguish between focus and selection by tap)

Everything is working correctly, except I'm trying to setup the first cell as focused on startup, without the user having to use tab/down key initially.

I've tried:

  • calling setNeedsFocusUpdate() and updateFocusIfNeeded()
  • setting 1st cell in preferredFocusEnvironments and indexPathForPreferredFocusedView
  • setting focusGroupPriority to different values (even Int.max) directly on the cell

But initial focus is not set.

Enabling focus loop debugger correctly shows the 1st cell as the 'next' focused element, but I cannot figure out how to actually trigger it without external keyboard.

Thanks

The focus system is purely reactive to user input. You can not force focus to be enabled by default automatically. The user always needs to engage with the keyboard first in order to prevent focus states hanging around when the user is not using their keyboard.

Also just to clarify, you said "I must distinguish between focus and selection by tap", that is not what selectionFollowsFocus is about. The user can always focus on something and then press the return key. You will not be able to distinguish this selection from a selection via tap. The difference with selectionFollowsFocus is that this selection happens automatically as soon as the cell becomes focused.

If you think it is important for your use case that you have focus active as soon as a hardware keyboard is attached, it would be great if you could file feedback for that, explaining why this is necessary in your case. (If you do, please post the feedback number here for reference)

How to initially enable focus in a collection view programmatically?
 
 
Q