Ios 按钮背景色不变

Ios 按钮背景色不变,ios,swift,uibutton,uibackgroundcolor,Ios,Swift,Uibutton,Uibackgroundcolor,我有以下自定义单元格: class MyCell: UITableViewCell { func configure(title1: String, title2: String) { backgroundColor = .red let myView = MyView(frame: frame) myView.button1.setTitle(title1, for: .normal) myView.button2.s

我有以下自定义单元格:

class MyCell: UITableViewCell {

    func configure(title1: String, title2: String) {
        backgroundColor = .red

        let myView = MyView(frame: frame)
        myView.button1.setTitle(title1, for: .normal)
        myView.button2.setTitle(title2, for: .normal)

        addSubview(myView)
    }
}
和自定义视图:

class MyView: UIView {
    var button1: UIButton!
    var button2: UIButton!

    override var backgroundColor: UIColor? {
        didSet {
            guard UIColor.clear.isEqual(backgroundColor) else { return }
            button1.setTitle("asldkfa")
            button1.backgroundColor = .blue
            button2.backgroundColor = .yellow
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        button1 = UIButton()
        button2 = UIButton()
        button1.backgroundColor = .purple
        button2.backgroundColor = .brown
        backgroundColor = .gray

        let stackView = UIStackView(); stackView.distribution = .fill; stackView.alignment = .fill
        stackView.addArrangedSubview(button1)
        stackView.addArrangedSubview(button2)

        addSubview(stackView)
    }
}
我在
tableView:cellForRowAt
方法中初始化了单元格视图,如下所示:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell
cell.configure(title1: "Foo", title2: "Bla")
我在tableView中获得以下输出:

每次外部更改
MyView
的颜色时(例如,单击单元格时),我都想更改按钮的颜色–这就是为什么我在
MyView
中覆盖了
didSet
背景色的观察者。最终我希望这些颜色是随机的,但这里我只是想改变
按钮1.backgroundColor=.blue

它不工作,按钮的颜色不变。我甚至尝试过更改
色彩
,但它也不起作用。使用
按钮1更改标题。setTitle(…)
确实有效,所以我不知道发生了什么

有人有主意吗

提前谢谢

编辑

当我构建应用程序时,在
didSet
中添加
button1.setTitle(“asldkfa”,代表:.normal)
,然后单击单元格,这是输出:

这意味着设置了
backgroundColor
,因为标题确实发生了变化,而不是颜色


重要:没有其他代码明确更改了
背景色
,甚至没有实现
didSelectRowAt
方法。当选择一个单元格时,其子视图的背景颜色会自动更新,因此这就是我现在通过选择单元格来更改颜色的方式。

事实上,问题是它没有设置您添加的颜色,因此在更改颜色时,它将保持不变,您应该这样做

 override var backgroundColor: UIColor? {
    didSet {
        button1.backgroundColor = backgroundColor
        button2.backgroundColor = backgroundColor
    }
}
更新:不是使用
ui按钮
backgroundColor
属性,而是实际要使用
setBackgroundImage(u:for:)
。您可以使用
UIImage
()上的扩展来从颜色创建图像,这对于您尝试执行的操作应该很有效

生成的配置代码应类似于:

button1.setBackgroundImage(.image(with: .red), for: .normal)

我看不到你的代码中有任何地方实际修改了myView的背景色。正如我在回答中提到的,只需单击单元格。
backgroundColor
已更改为所选颜色。请显示您的选择代码好吗?好的,现在我看到问题了。更改单元格的背景色并不意味着更改作为子视图添加到单元格中的myView的背景色。您需要在tableView单元格中重写
setSelected
,或者使用
didSelectRowAt
delegate方法并以编程方式更改
myView
的背景色。我在示例中成功地重新创建了该行为,但仍然无法了解那里发生了什么。在选择时,视图的背景色似乎完全消失了,将其更改为零,然后返回到灰色。但我不想要
背景色,我想要另一种随机颜色。你不能这样做,这样做没有任何意义,因为在初始化你更改颜色时。我不关心初始颜色,我关心的是在superview更改其自身颜色时更改颜色。我该怎么做?仅供参考:这是可能的,只需在调用super.init(frame:)后,在MyView的初始值设定项中为属性设置值,而不是设置
backgroundColor
您已经触发了didSet
——这部分背景颜色可以根据需要随时更改(将是随机颜色),因此这不是问题所在。我没有选择单元格的代码。稍后将测试此代码。谢谢。您可能只是在示例代码中忽略了这一点,但正如@Eyal所提到的,我们也看不到您将StackView添加为子视图的位置。请确保您没有遗漏:)我忽略了该代码,因为我认为它不相关。我现在已经添加了它。我也不会每次都更改颜色,只要选择单元格(
backgroundColor==UIColor.clear
)。我用你的答案更新的代码就在这里,而且它也不能仅仅改变按钮的背景颜色。我刚刚发布了一个更新(#2)abve-之前遗漏了一些东西,这很可能是问题所在。