简体   繁体   English

无法设置UILabel的textColor

[英]Can't set textColor of UILabel

I'm currently working on a dynamic form that's originated from UITableViewCells Xibs. 我目前正在处理源自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
}

The UILabel ( passTitleLabel ) keeps the color set on the Storyboard file and doesn't change as expected. UILabel( passTitleLabel )保留在Storyboard文件上设置的颜色,并且不会按预期更改。 The UILabel is enabled and not highlighted but it is still preserving the color on the storyboard. UILabel已启用且未突出显示,但仍保留情节提要板上的颜色。

All the colors are working in other classes (they're on Assets.xcassets ). 所有颜色都在其他类中起作用(它们位于Assets.xcassets )。 The Label is properly set with an IBOutlet. 标签已通过IBOutlet正确设置。

Ran into the same issue, took me a while but finally found an answer. 遇到同样的问题,花了我一段时间,但终于找到了答案。 Looks like your UITableViewCell or UICollectionViewCell is running in a background thread, UI related operations need to be done in the main thread or it can lead to these type of problems. 看起来您的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
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM