Button is not focusable because it is out of reach - tvOS Swift

579 views Asked by At

I am creating a tvOS movie browser. This view holds the movie description and displays 4 things: the movie cover image, the movie description text, a watch button, and a collection view of the movie's cast (blue boxes). The preferredFocusView is set the the watchButton. However, once I switch focus to the collection view below it, I am not able to focus on the watch button again if the cast is too short.

The watch button is focusable here since the cast extends to just below the button

The watch button IS NOT focusable here

Ultimately, I know I can fix it by rearranging the UI so that the watch button is directly above the first cast member, but I want to know if there is an actual way to achieve the original look I was going for.

Thanks!

1

There are 1 answers

0
iOess On

To fix this, I added a focus guide to my view controller.

private var focusGuide: UIFocusGuide!

and in viewDidLoad:

// Create focus guide
focusGuide = UIFocusGuide()
view.addLayoutGuide(focusGuide)

focusGuide.leftAnchor.constraint(equalTo: header.imageView.leftAnchor).isActive = true
focusGuide.bottomAnchor.constraint(equalTo: header.imageView.bottomAnchor).isActive = true
focusGuide.widthAnchor.constraint(equalTo: header.imageView.widthAnchor).isActive = true
focusGuide.heightAnchor.constraint(equalTo: header.watchButton.heightAnchor).isActive = true