我有一个在应用程序中显示时间段的集合视图。在暗模式下,UILabel似乎无法在白色背景上显示黑色文本颜色。
在故事板中,我已将标签的颜色设置为黑色(也尝试了默认颜色)。
在代码中,当用户选择单元格时,
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? TimeCell{
cell.timeLabel.toggleTheme(true)
}
}
而我有UILabel的扩展:
extension UILabel{
func toggleTheme(_ selected : Bool){
if selected{
if #available(iOS 13.0, *) {
if self.traitCollection.userInterfaceStyle == .dark{
self.textColor = UIColor.black
self.backgroundColor = UIColor.white
}else{
self.textColor = UIColor.white
self.backgroundColor = UIColor.black
}
} else {
self.textColor = UIColor.white
self.backgroundColor = UIColor.black
}
}else{
if #available(iOS 13.0, *) {
if self.traitCollection.userInterfaceStyle == .dark{
self.textColor = UIColor.white
self.backgroundColor = UIColor.black
}else{
self.textColor = UIColor.black
self.backgroundColor = UIColor.white
}
} else {
self.textColor = UIColor.black
self.backgroundColor = UIColor.white
}
}
}
}
结果如下: