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

后端 未结 6 2272
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:38

    Here's a nice little Swift 2 copy / paste snippet:

    var myPreferredFocusedView: UIView?
    override var preferredFocusedView: UIView? {
        return myPreferredFocusedView
    }
    func updateFocus(to view: UIView) {
        myPreferredFocusedView = napDoneView
        setNeedsFocusUpdate()
        updateFocusIfNeeded()
    }
    

    Use it like this:

    updateFocus(to: someAwesomeView)
    

提交回复
热议问题