无法设置UILabel的文本颜色

问题描述 投票:2回答:1

我目前正在开发一个源自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正确设置。

ios swift uilabel uicolor
1个回答
6
投票

遇到同样的问题,花了我一会儿,但终于找到了答案。看起来你的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
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.