Search code examples
uibuttontvos

Move focus to other UIButton


I am working on a small game for Apple TV, and one thing I can't get to work is moving the focus from one button to the other (programmatically), or temporarily remove focus from any object (buttons) as the game is doing stuff.

I've seen the guide about objects and focus, but is there really no way to programmatically move the focus to an other part of the screen as the input is needed there (instead of letting the user move all the way across the screen)?


Solution

  • There will always be a view that has focus. It's not possible to not have a focused view.

    You could temporarily change the focused appearance of an item, so it doesn't appear to have focus, but that would likely be be confusing for the user, or conflict with Human Interface Guidelines.

    There's no explicit way to programmatically move focus from one control to another, per the App Programming Guide for tvOS:

    The Focus Engine Controls Focus

    Only the focus engine can explicitly update focus, meaning there is no API for directly setting the focused view or moving focus in a certain direction. ...

    The focus engine controls focus to make sure that it does not move around the screen unexpectedly, and that it behaves similarly across different applications.

    Answers to other questions have suggested that you could "game" the system by overriding preferredFocusedView, conditionally setting it to the desired control to move to, then requesting a focus update. Such an approach would likely be fragile.