Search code examples
objective-ctvos

UITableView: Change selected cell background color


I am not able to change the white background when navigating in a UITableView on tvOS.

I edited the background / text color on the whole table for design purposes so the white selected background is annoying.

I tried:

cell.selectedBackgroundView.backgroundColor = [UIColor blackColor];

But it doesn't update the highlighted background color.


Solution

  • tvOS use focus to show the current selected item. To change the backgroundColor, override the didUpdateFocusInContext() method in your cell class.

    public override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        context.previouslyFocusedView?.backgroundColor = UIColor.clearColor()
        context.nextFocusedView?.backgroundColor = UIColor.blackColor()
    }