简体   繁体   中英

Can't set textColor of UILabel

I'm currently working on a dynamic form that's originated from 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. The UILabel is enabled and not highlighted but it is still preserving the color on the storyboard.

All the colors are working in other classes (they're on Assets.xcassets ). The Label is properly set with an 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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