swifttvosavplayerviewcontrollerfocus-engine

tvOS Focus button in a Subview


I am using AVPlayerController and I have added a subview on top of AVPlayerController. As Soon the subview is displayed I want to shift focus from the player to the Subview and focus the UIButton added on top of that Subivew. I have tried preferredfocusview but its a read-only property and I cannot change it. Can anyone pelase assist. Thanks a lot.


Solution

  • First of all, preferredFocusView is deprecated so you should use preferredFocusEnvironments instead.

    It is a computed property, which means you don't assign to it, you override it:

    override var preferredFocusEnvironments: [UIFocusEnvironment] {
    //if subview exists, return the subview or the button, whichever is focusable
        return [subview]
    //otherwise use the default implementation of AVPlayerController
        return super.preferredFocusEnvironments
    }
    

    You should also call self.setNeedsFocusUpdate() and self.updateFocusIfNeeded() to request a focus update when you add the subview.