如何在tvOS中设置TableViewCell焦点颜色

时间:2015-12-08 06:35:06

标签: objective-c focus tvos

UITableViewCell的默认焦点颜色为白色。 我们如何改变UITableView单元格的焦点颜色?

4 个答案:

答案 0 :(得分:1)

这是一个快速的代码。请转换为Objective-C并尝试一下。它可能对你有帮助。

<Grid>
    <Button Name="Button1" Content="1" Click="Button1_OnClick"/>
    <Button Name="nr1"  Visibility="Hidden" Click="Nr1_OnClick">
        <Button.Template>
            <ControlTemplate>
                <Image  Source="YourImage" Stretch="Fill" />
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>


private void Button1_OnClick(object sender, RoutedEventArgs e)
    {
        if (nr1.Visibility == Visibility.Visible)
        {
            nr1.Visibility = Visibility.Hidden;
            Button1.Visibility = Visibility.Visible;
        }
        else
        {

            nr1.Visibility = Visibility.Visible;
            Button1.Visibility = Visibility.Hidden;
        }
    }

    private void Nr1_OnClick(object sender, RoutedEventArgs e)
    {
        if (Button1.Visibility == Visibility.Visible)
        {
            Button1.Visibility = Visibility.Hidden;
            nr1.Visibility = Visibility.Visible;
        }
        else
        {
            nr1.Visibility = Visibility.Hidden;
            Button1.Visibility = Visibility.Visible;
        }
    }

看一下屏幕截图。

http://localhost:8081/index.android.bundle?platform=android initial focus image Second focus image

答案 1 :(得分:1)

如果您想维护花哨的视差细胞,可以先在自定义细胞上设置背景视图:

  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()
    }
  }

在带有tableview的viewcontroller中执行以下操作:

triangles()

答案 2 :(得分:0)

如果您使用的是自定义tableViewCell,则可以使用tableViewCell的委托方法,如下所示,

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    if (self.focused)
    {
        [self showHighlightedCellStyle];//set highlighted bg color
    }
    else
    {
        [self showNormalCellStyle];//reset the bg color
    }
}

答案 3 :(得分:0)

您可以按照以下代码进行设置:

- (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];
}