How can I force focus to change to a specific view in tvOS?

Simen Øian Gjermundsen picture Simen Øian Gjermundsen · Sep 24, 2015 · Viewed 17.8k times · Source

I am implementing custom code to handle a click on the Menu button on the Siri Remote. How can I force focus to change to my custom menu when pressing the menu button?

Answer

vikas prajapati picture vikas prajapati · Nov 18, 2016

For ios 10 you should use preferredFocusEnvironments instead of preferredFocusedView .

In below example if you want to focus on button then see below code.

@IBOutlet weak var button: UIButton!

override var preferredFocusEnvironments: [UIFocusEnvironment] {
    return [button]
}

override func viewDidLoad() {
    super.viewDidLoad()

    setNeedsFocusUpdate()
    updateFocusIfNeeded()
}