首页 文章

无法设置UILabel的textColor

提问于
浏览
2

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

1 回答

  • 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
       }
    }
    

相关问题