美文网首页
iOS小笔记 | cell highlight 时子控件背景颜色

iOS小笔记 | cell highlight 时子控件背景颜色

作者: 无夜之星辰 | 来源:发表于2019-08-02 20:52 被阅读0次
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

更多方案可参考:
https://stackoverflow.com/questions/2965085/uitableviewcell-makes-labels-background-clear-when-highlighted

相关文章

网友评论

      本文标题:iOS小笔记 | cell highlight 时子控件背景颜色

      本文链接:https://www.haomeiwen.com/subject/qypwlctx.html