IU的衣服真多
问题描述
如GIF所示,点按cell,cell进入highlight状态,此时它上面的一个橙色label背景颜色消失了:
cell点击时子控件的背景颜色消失.gif
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")!
背景颜色不会消失了.gif









网友评论