ios - 无法设置 UILabel 的 textColor
问题描述
我目前正在研究源自 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 正确设置标签。
解决方案
遇到同样的问题,花了我一段时间,但终于找到了答案。看起来您的 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
}
}
推荐阅读
- javascript - 想要以某种预期的格式对密码的值进行编码
- javascript - 如何在 Android 的 React Native 中以编程方式打开 Tez 应用(印度的 Google Pay 应用)?
- python - python 代码覆盖错误 cat: /ade_info.vmd: No such file or directory
- c - 关于 nftw() 和 ftw() 线程安全的问题
- javascript - Javascript - 不加载
- visual-studio-code - VSCode 扩展。将类型注入无标题文件
- php - 从 php 到 mysql 的时间格式
- angular - 带有 Angular Service Worker 的 Angular Universal
- salesforce - 我们如何在 Salesforce 的 IF 语句中覆盖多个条件的代码覆盖率
- c# - 以当前登录以外的其他用户身份使用 WPF WebBrowser 运行应用程序 - 某些网页无法正常工作