How can I disable the default tvOS focus animations?

2.2k Views Asked by At

I need to create a UI which is a mix of UITableView, UITextField, and some custom UIView's and I also need to provide a custom focus animation.

How can I get the UITableView/UITableViewCell and UITextField to not render the default focus animations?

I tried:

    override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        if let view = context.previouslyFocusedView {
            view.layer.removeAllAnimations()
        }
        if let view = context.nextFocusedView {
            view.layer.removeAllAnimations()
        }
    }

I can add my own animations, but can't get rid of the 'default' animations from the view.

3

There are 3 best solutions below

0
On

Change the focus style of the cell to custom instead of default.

cell.focusStyle = UITableViewCellFocusStyle.custom
1
On

To remove focus from UITableViewCells, use this code:

 func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
    return false
}
0
On

I've solved my problem with which is similar to your question but my UITableView which has just one UITableViewCell. So when UIViewController present / load there is no focused UITableViewCell and when I click cell it perform its action ( present an UIAlertController)

    if let focusedCell = UIScreen.main.focusedView as? MyCell{

         focusedCell.backgroundColor = UIColor.clear
    }

then

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // take action
    ... alertController configuration
    present(alertController, animated: true, completion: {

          tableView.reloadData()

    })

}

When table has reloaded itself it convert its not focused form and the cycle is repeat itself.