Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move the focus from a button to another in tvOS?

I'm new to tvOS. I would like to have a standard button that once is pressed, it moves the focus to another standard button, how can I do this (if it's possible of course)?

like image 825
Cue Avatar asked Oct 12 '15 12:10

Cue


2 Answers

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

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

Then, in your button callback, set the myPreferredFocusedView to your 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 your other button.

like image 103
Antoine Avatar answered Sep 23 '22 20:09

Antoine


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

buttonB.setNeedsFocusUpdate()
viewController.setNeedsFocusUpdate()

This only works if they're on the same level in the view hierarchy, though. If not then you probably need to chain the calls to setNeedsFocusUpdate all the way up the hierarchy or something...

Read more here

like image 24
CMash Avatar answered Sep 19 '22 20:09

CMash