【问题标题】:UILabel and UITableViewCell background colours coming out differentUILabel 和 UITableViewCell 背景颜色不同
【发布时间】:2013-11-26 15:32:30
【问题描述】:

我正在尝试使用代码为我的应用程序设置通用配色方案,以便在项目之间移植时轻松修改它。但是,这些组件上的两种颜色不断出现略有不同的颜色。我使用以下代码来设置我的 UITableViewCell:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if ([indexPath row] %2 != 0)
    {
        [cell setBackgroundColor:kTableSecondaryCellColor];
    }
}

这是我的 UILabel:

[self.lTitleBackground setBackgroundColor:kTableSecondaryCellColor];

如您所见,我只是将两个元素的背景设置为 kTableSecondaryCellColor,设置如下:

#define kTableSecondaryCellColor [UIColor colorWithRed:(158.0f / 256.0f) green:(171.0f / 256.0f) blue:(4.0f / 256.0f) alpha:0.4f] 

但是,两种颜色的结果如下(绿色):

【问题讨论】:

  • 你可以尝试设置cell.contentView.backgroundColor = kTableSecondaryCellColor;我认为这可能会导致问题。
  • 完美,这几天一直困扰着我。您能否将其添加为答案,以便我接受。

标签: objective-c uitableview colors uilabel


【解决方案1】:

你必须使用

cell.contentView.backgroundColor = kTableSecondaryCellColor;

【讨论】:

    【解决方案2】:

    您应该更改单元格内容视图的背景颜色:

    cell.contentView.backgroundColor = kTableSecondaryCellColor;
    

    这应该可以解决问题。

    【讨论】:

    • 嗨,格雷格,我想知道你是否可以帮助我。这似乎不适用于 iPad,由于某种原因它仍然忽略不透明度元素。你知道为什么会这样吗?
    • @ElliottD'Alvarez 它应该适用于两者。你确定你有完全相同的代码吗?
    • @ElliottD'Alvarez 如果代码完全相同,我不知道为什么这可能值得创建另一个问题。
    【解决方案3】:
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {    
        if ([indexPath row] %2 == 0)
        {
            cell.contentView.backgroundColor = kColorTablePrimary;
        }
        else
        {
            cell.contentView.backgroundColor = kColorTableSecondary;
        }
    }
    

    这是我目前正在使用的代码,仅在 iPhone 上检测到不透明度,但我在此代码中有一个断点,当我使用 iPad 时肯定会调用它,只是不显示不透明度。我已经完成了设置,并且单元格的设置都相同,与 UITableViews 相同。

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2010-09-18
      • 2011-12-17
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 2023-03-03
      • 2015-07-21
      • 1970-01-01
      相关资源
      最近更新 更多