【问题标题】:Can't set textColor of UILabel无法设置 UILabel 的 textColor
【发布时间】:2018-04-30 20:25:43
【问题描述】:

我目前正在处理源自 UITableViewCells Xibs 的动态表单。

if disabled {
    self.passTitleLabel.textColor = UIColor(named: "Dark Blue")
    self.textField.textColor = UIColor(named: "Dark Blue")
} else {
    self.passTitleLabel.textColor = UIColor(named: "Light Blue")
    self.textField.textColor = .white
}

UILabel (passTitleLabel) 保留 Storyboard 文件中设置的颜色,并且不会按预期更改。 UILabel 已启用且未突出显示,但仍保留情节提要上的颜色。

所有颜色都在其他类中工作(它们在Assets.xcassets)。使用 IBOutlet 正确设置了标签。

【问题讨论】:

  • 您发布的这段代码在哪里?
  • 将你的行分成两行。例如,let color = UIColor(named: "Dark Blue") 然后self.passTitleLabel.textColor = color。确保 color 在运行时不是 nil
  • @rmaddy 调用单元格更新时正在调用代码,就像表单的其他字段一样。我确实分成了2行,还是一样的。谢谢:)
  • 我猜passTitleLabel outlet 连接到的标签与您想象的不同。
  • 分割线时,color 是否为零?

标签: ios swift uilabel uicolor


【解决方案1】:

遇到了同样的问题,花了我一段时间,但终于找到了答案。看起来您的 UITableViewCell 或 UICollectionViewCell 正在后台线程中运行,UI 相关操作需要在主线程中完成,否则会导致此类问题。

if disabled {
   DispatchQueue.main.async {
      self.passTitleLabel.textColor = UIColor(named: "Dark Blue")
      self.textField.textColor = UIColor(named: "Dark Blue")
   }
} else {
   DispatchQueue.main.async {
      self.passTitleLabel.textColor = UIColor(named: "Light Blue")
      self.textField.textColor = .white
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多