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