设置customView的背景颜色无效

时间:2020-04-02 21:05:57

标签: ios swift uiview background-color

我有一个customView,在button-tap之后显示。我的问题是,设置.backgroundColor没有任何效果,而只是一个clear背景。

CustomView:

let wishWishView: WishStackView = {
    let v = WishStackView()
    v.backgroundColor = .cyan
    v.translatesAutoresizingMaskIntoConstraints = false
    return v
}()

AddViewButton:

@objc func addWishButtonTapped(){

    self.view.addSubview(self.wishWishView)
    wishWishView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
    wishWishView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

}    

WishWishView只是一个简单的UIView,其中包含StackView

2 个答案:

答案 0 :(得分:2)

我的猜测是它是UIStackView,它不支持具有背景色。

您可以在此处详细了解为什么会发生这种情况,以及可能的解决方法:https://stackoverflow.com/a/34868367/3992237

答案 1 :(得分:0)

您的代码完全错误,首先您要这样设置视图:

let wishWishView: UIView = {
    let v = UIView()
    v.backgroundColor = .cyan
    v.translatesAutoresizingMaskIntoConstraints = false
    return v
}()

现在设置您的stackView,在我的示例中,我在垂直轴上放置了2个标签:

let stackView: UIStackView = {
    let label1 = UILabel()
    label1.text = "Label 1"
    label1.textColor = .red
    label1.font = UIFont.systemFont(ofSize: 16)

    let label2 = UILabel()
    label2.text = "Label 2"
    label2.textColor = .black
    label2.font = UIFont.systemFont(ofSize: 16)

    let sV = UIStackView(arrangedSubviews: [label1, label2])
    sV.axis = .vertical
    sV.distribution = .fillEqually
    sV.translatesAutoresizingMaskIntoConstraints = false

    return sV
}()

在那之后设置您的按钮:

let buttonAddView: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Add View", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.backgroundColor = .red
    button.addTarget(self, action: #selector(addWishButtonTapped), for: .touchUpInside)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

在viewDidLoad中添加按钮并设置约束

view.addSubview(buttonAddView)
    buttonAddView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
    buttonAddView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
    buttonAddView.heightAnchor.constraint(equalToConstant: 50).isActive = true
    buttonAddView.widthAnchor.constraint(equalToConstant: 120).isActive = true

现在编写一个函数,该函数在用正确的约束轻击按钮时在内部添加带有堆栈视图的视图,在您的函数中,您仅调用视图的位置,而不调用视图的大小。这样编写函数:

@objc func addWishButtonTapped(){

    view.addSubview(self.wishWishView)
    wishWishView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
    wishWishView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    wishWishView.heightAnchor.constraint(equalToConstant: 100).isActive = true
    wishWishView.widthAnchor.constraint(equalToConstant: 200).isActive = true

    wishWishView.addSubview(stackView)
    stackView.topAnchor.constraint(equalTo: wishWishView.topAnchor).isActive = true
    stackView.bottomAnchor.constraint(equalTo: wishWishView.bottomAnchor).isActive = true
    stackView.leadingAnchor.constraint(equalTo: wishWishView.leadingAnchor, constant: 10).isActive = true
    stackView.trailingAnchor.constraint(equalTo: wishWishView.trailingAnchor, constant: -10).isActive = true

}

现在只需更改wishWishView的背景以自动设置stackView背景,就是这样...