【问题标题】:How to Set TableViewCell Focus colour in tvOS如何在 tvOS 中设置 TableViewCell 焦点颜色
【发布时间】:2015-12-08 06:35:05
【问题描述】:

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

【问题讨论】:

    标签: objective-c focus tvos


    【解决方案1】:

    您可以在下面的代码中进行设置:

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

    【讨论】:

      【解决方案2】:

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

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

      看一下屏幕截图。

      【讨论】:

      • 在 Objective C 中尝试过相同...但单元格的焦点颜色仍然没有改变
      • 你在哪里写的这段代码,在我这边它工作正常。您将把这段代码写到自定义单元类中。
      • 自定义单元格背景发生变化...但是tableviewcell(parallax)的默认焦点样式动画消失了。
      • 看看这个链接可能对你有帮助。 stackoverflow.com/questions/31373775/…
      • 感谢您的帮助...我找到了另一种解决方案。我在自定义单元格上添加了一个视图并将其映射到单元格背景视图。因此,当调用 tableview 焦点更新方法时,我只需更改背景视图颜色...然后颜色随默认 tableviewcell 视差动画而变化
      【解决方案3】:

      如果您想保持精美的视差单元格,您可以先在自定义单元格上设置背景视图:

      self.backgroundView = UIView()
      

      在你的 viewcontroller 和 tableview 中做这样的事情:

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

      【讨论】:

        【解决方案4】:

        如果您使用自定义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
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-29
          • 2017-06-17
          • 1970-01-01
          • 2021-03-12
          相关资源
          最近更新 更多