How to transfer focus from one button to another in tvOS?

I am new to tvOS. I would like the standard button to be pressed, it moves the focus to another standard button, how can I do this (if possible, of course)?

+5
source share
2 answers

Start by overriding preferredFocusedView in your viewController with a custom property:

 var myPreferredFocusedView:UIView? override var preferredFocusedView: UIView? { return myPreferredFocusedView:UIView } 

Then, in the button myPreferredFocusedView set myPreferredFocusedView to the next preferred button, which should get focus. After that, directly request a focus update:

 func buttonCallback(){ myPreferredFocusedView = button2 // button2 is an example /* Trigger the focus system to re-query the view hierarchy for preferred focused views. */ setNeedsFocusUpdate() updateFocusIfNeeded() } 

This should update the focus system to another button.

+10
source

Another option is to do something like the following when buttonA (for example) has focus:

 buttonB.setNeedsFocusUpdate() viewController.setNeedsFocusUpdate() 

This only works if they are at the same level in the hierarchy of views. If not, then you probably need to associate calls with setNeedsFocusUpdate to a hierarchy or something else ...

More here

+1
source

Source: https://habr.com/ru/post/1233522/


All Articles