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

后端 未结 6 2194
Happy的楠姐
Happy的楠姐 2021-02-19 15:51

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?

6条回答
  •  佛祖请我去吃肉
    2021-02-19 16:45

    Finally figured it out myself. You have to override the preferredFocusedView property of your UIView or UIViewController.

    In Swift it works like this:

    func myClickHandler() {
        someCondition = true
        self.setNeedsFocusUpdate()
        self.updateFocusIfNeeded()
        someCondition = false
    }
    
    override weak var preferredFocusedView: UIView? {
        if someCondition {
            return theViewYouWant
        } else {
            return defaultView
        }
    }
    

    I can't quite remember how to override getters in Objective-C so if someone want to post that I'll edit the answer.

提交回复
热议问题