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

后端 未结 6 2304
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:43

    Here is another implementation based on Slayters answer above. Its slightly more elegant I think than using the conditional booleans.

    Put this in your viewcontroller

    var viewToFocus: UIView? = nil {
        didSet {
            if viewToFocus != nil {
                self.setNeedsFocusUpdate();
                self.updateFocusIfNeeded();
            }
        }
    }
    
    override weak var preferredFocusedView: UIView? {
        if viewToFocus != nil {
            return viewToFocus;
        } else {
            return super.preferredFocusedView;
        }
    }
    

    Then to use it in your code

    viewToFocus = myUIView;
    

提交回复
热议问题