-1

I have a stackview in which there are 3 items vertically aligned. Now I created a Uiview to give it a border and adding that too in stackview to show it as a colored background of with radius corners.

I am successful of creating and attaching a view to the UIStackview. But it seems like it is covering all other arranged subviews of UIStackView. whereas I want to bring other subviews at front. please help me. here is the code below:

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
        }

    }
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Android teem
  • 780
  • 2
  • 13
  • 36

2 Answers2

1

You can try this

self.insertSubview(subView, at:0)

instead of

self.addSubview(subView)
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

You can use self.view.bringSubview(toFront: yourView)

João Fernandes
  • 491
  • 7
  • 17