
问题描述
如GIF所示,点按cell,cell进入highlight状态,此时它上面的一个橙色label背景颜色消失了:

button和label都会出现这种情况。
解决方案
因为这个项目大量用到基类,即使连label和button这种基础控件都写了基类,虽然我对此是持吐槽态度,但这个时候正好可以为我所用:
在基类里重写backgroundColor
属性,每次给backgroundColor
赋值的时候,都给它设置我们期望的color——
/// 设置此属性后,cell highlight 时此label的背景颜色不会变化
var persistentBackgroundColor: UIColor? {
didSet {
super.backgroundColor = persistentBackgroundColor
}
}
override var backgroundColor: UIColor? {
didSet {
if let persistentColor = self.persistentBackgroundColor {
if backgroundColor != persistentColor {
backgroundColor = persistentColor
}
}
}
}
在需要的地方设置persistentBackgroundColor
即可:
self.identifierLabel.persistentBackgroundColor = UIColor.hexColor("#F57E49")!


网友评论