Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在后台添加子视图_Ios_Swift_Uiview_Uistackview - Fatal编程技术网

Ios 如何在后台添加子视图

Ios 如何在后台添加子视图,ios,swift,uiview,uistackview,Ios,Swift,Uiview,Uistackview,我有一个stackview,其中有3个项目垂直对齐。现在,我创建了一个Uiview给它一个边框,并在stackview中添加了边框,将其显示为带半径角的彩色背景 我成功地创建了视图并将其附加到UIStackview。但它似乎覆盖了UIStackView的所有其他排列子视图。而我想把其他子视图放在前面。请帮帮我。代码如下: extension UIStackView { override open var backgroundColor: UIColor? { get {

我有一个stackview,其中有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
        }

    }
}
你可以试试这个

self.insertSubview(subView, at:0)
而不是

self.addSubview(subView)

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

子视图.layer.zPosition=-1的可能重复项