I have a UIView as a “dialog” with various buttons (similar to an Alertbox)… in tvOS I need to move the focus to one of the buttons when it is presented… I’m doing what every web article says, but something is not right
func present() {
... setup stuff
viewToFocus(to:actions[0]) // this 'should' focus that button
}
var myPreferredFocusedView: UIView?
func viewToFocus(to : UIView) {
myPreferredFocusedView = to
print("TO :\(myPreferredFocusedView! )")
print("CAN: \(myPreferredFocusedView!.canBecomeFocused)")
self.setNeedsFocusUpdate()
self.updateFocusIfNeeded()
print("IS : \(myPreferredFocusedView!.isFocused)")
}
///output from above routine
///TO:<atvSOLITAIRE.UXAlertAction: 0x11871bcf0; ...
///CAN: true // indicates the button CAN be focuses
///IS: false // but it is not
///the below routine NEVER gets called, and should via self.updateFocusIfNeeded()
override var preferredFocusEnvironments: [UIFocusEnvironment] {
print("Prefer: \(myPreferredFocusedView)")
if myPreferredFocusedView != nil {
return [myPreferredFocusedView!]
} else {
return super.preferredFocusEnvironments
}
}