Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set TableViewCell Focus colour in tvOS

Default focus color of UITableViewCell is white colour. How can we change the focus colour of UITableView cell?

like image 801
GAURAV VIG Avatar asked Dec 08 '15 06:12

GAURAV VIG


4 Answers

You can set this as in the code below:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    cell.focusStyle = UITableViewCellFocusStyleCustom;
    // ...
    return cell;
}

- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    if ([context.previouslyFocusedView isKindOfClass:[UITableViewCell class]])
        context.previouslyFocusedView.backgroundColor = [UIColor clearColor];
    if ([context.nextFocusedView isKindOfClass:[UITableViewCell class]])
        context.nextFocusedView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
}
like image 162
Leszek Szary Avatar answered Oct 31 '22 17:10

Leszek Szary


It is a swift code. Please convert to Objective-C and try it. It maybe help you.

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

if let nextFoc = context.nextFocusedView as? YourCellName{

       nextFoc.backgroundColor = UIColor.redColor()

}

if let prevFocus = context.previouslyFocusedView as? YourCellName{

        prevFocus.backgroundColor = UIColor.clearColor()

}

Look it a screen shot.

initial focus imageSecond focus imageThird focus image

like image 34
Sankalap Yaduraj Singh Avatar answered Oct 31 '22 19:10

Sankalap Yaduraj Singh


If you want to maintain the fancy parallax cells you can first set a background view on your custom cell:

self.backgroundView = UIView()

And in your viewcontroller with the tableview do something like this:

  func tableView(tableView: UITableView, didUpdateFocusInContext context: UITableViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    (context.nextFocusedView as! MyCustomTableviewCell).backgroundView!.backgroundColor = UIColor.purpleColor()
    if let prev = context.previouslyFocusedView as? MyCustomTableviewCell {
      // Set the color back to whatever it was, in this case I have a black background with black cells
      prev.backgroundView?.backgroundColor = UIColor.blackColor()
    }
  }
like image 1
CodyMace Avatar answered Oct 31 '22 19:10

CodyMace


If you are using custom tableViewCell, then you can use the delegate method of the tableViewCell as below,

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    if (self.focused)
    {
        [self showHighlightedCellStyle];//set highlighted bg color
    }
    else
    {
        [self showNormalCellStyle];//reset the bg color
    }
}
like image 1
ramgandhi Avatar answered Oct 31 '22 19:10

ramgandhi