【问题标题】:how to add subview at the background如何在后台添加子视图
【发布时间】:2018-08-02 11:07:09
【问题描述】:

我有一个堆栈视图,其中有 3 个项目垂直对齐。现在我创建了一个Uiview 给它一个边框,并在stackview 中添加它以将其显示为带有半径角的彩色背景。

我成功地创建了一个视图并将其附加到UIStackview。但它似乎涵盖了UIStackView 的所有其他排列的子视图。而我想把其他子视图放在前面。请帮我。这是下面的代码:

extension UIStackView {
    override open var backgroundColor: UIColor? {

        get {
            return super.backgroundColor
        }

        set {

            super.backgroundColor = newValue

            let tag = -9999
            for view in subviews where view.tag == tag {
                view.removeFromSuperview()
            }

            let subView = UIView()
            subView.tag = tag
            subView.backgroundColor = newValue
            subView.translatesAutoresizingMaskIntoConstraints = false
            subView.layer.cornerRadius = 5
            subView.layer.masksToBounds = true
            subView.layer.borderColor = CommonUtils.hexStringToUIColor(hex: "#C2C2C2").cgColor
            subView.layer.borderWidth = 0.35
            self.addSubview(subView)
            subView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            subView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
            subView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
            subView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        }

    }
}

【问题讨论】:

标签: ios swift uiview uistackview


【解决方案1】:

你可以试试这个

self.insertSubview(subView, at:0)

而不是

self.addSubview(subView)

【讨论】:

    【解决方案2】:

    您可以使用self.view.bringSubview(toFront: yourView)

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多