user45723
user45723

Reputation: 123

Focus problem on tvOS app when moving from a view to another

Hy,

I have a view on tvOS that contains two views of different sizes. When I move from the left view I want to focus the items from the left view, but this is not happening.

Here is how it looks:View

The left view is a tableView and the right view is a UIView that has a textField on top. I want when I move from tableview to focus on textField. But now it does not happen.

I created a focusGuide on the mainController that contains those two views that has the same anchors as mainController and it doesn't work. Like this:

 private func setupFocusGuide(){
    self.sideFocusGuide = UIFocusGuide()
    view.addLayoutGuide(self.sideFocusGuide!)
    self.sideFocusGuide?.topAnchor.constraint(equalTo: self.mainView.topAnchor, constant: 0).isActive = true
    self.sideFocusGuide?.bottomAnchor.constraint(equalTo: self.mainView.bottomAnchor, constant: 0).isActive = true
    self.sideFocusGuide?.leftAnchor.constraint(equalTo: self.mainView.leftAnchor).isActive = true
    self.sideFocusGuide?.rightAnchor.constraint(equalTo: self.mainView.rightAnchor).isActive = true
    self.sideFocusGuide?.preferredFocusEnvironments = [self.rightView]
}

Upvotes: 1

Views: 688

Answers (1)

Roman Podymov
Roman Podymov

Reputation: 4521

I think you should use another UIFocusGuide that switches focus between UITableView and UITextField:

private var focusGuideTextFieldAndTableView: UIFocusGuide!

Initialise it:

focusGuideTextFieldAndTableView = UIFocusGuide()
view.addLayoutGuide(focusGuideTextFieldAndTableView)
focusGuideTextFieldAndTableView.leftAnchor.constraint(equalTo: tableView.rightAnchor).isActive = true
focusGuideTextFieldAndTableView.topAnchor.constraint(equalTo: tableView.topAnchor).isActive = true
focusGuideTextFieldAndTableView.bottomAnchor.constraint(equalTo: tableView.bottomAnchor).isActive = true
focusGuideTextFieldAndTableView.rightAnchor.constraint(equalTo: textField.leftAnchor).isActive = true
focusGuideTextFieldAndTableView.preferredFocusEnvironments = [textField]

Switch preferredFocusEnvironments in didUpdateFocus(in:with:):

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    super.didUpdateFocus(in: context, with: coordinator)
    if context.nextFocusedView == textField {
        focusGuideTextFieldAndTableView.preferredFocusEnvironments = [tableView]
    } else if context.previouslyFocusedView == textField {
        focusGuideTextFieldAndTableView.preferredFocusEnvironments = [textField]
    }
}

Upvotes: 1

Related Questions