Thursday, September 29, 2022

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

Issue

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)?


Solution

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.



Answered By - Antoine
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.