如何删除视图和更新约束?

How to remove a view and update constraints?

我有一个视图,其中我正在通过 xib 在添加按钮上以编程方式创建另一个视图。我可以在点击添加更多按钮时创建多个视图,如果我删除最后一个视图,则删除也可以正常工作,但是由于缺少约束,中间视图会出现问题视图未正确更新下面是图像的外观

初始视图是这样的

添加更多视图后

移除中间视图后

删除按钮代码

@IBAction func deletebnt(_ sender: UIButton) {
        let view = self.superview
        let index =  view?.subviews.index(of:self)!
        delegate.txtcheck(text: countstr)
         self.view.removeFromSuperview()
    }

添加按钮代码

@IBAction func addMoreBnt(_ sender: UIButton) {
      for constraint in addSuperview.constraints {
            if constraint.firstAttribute == NSLayoutAttribute.height
            {
                constraint.constant +=  45
                space = constraint.constant
            }
        }
        let newView : AvalabileTimeView = AvalabileTimeView()
        newView.frame = CGRect(x: self.addsubView.frame.origin.x, y: 70, width: addsubView.frame.size.width, height:addsubView.frame.size.height)
        newView.delegate = self as AvalabileTimeDelegate
        addSuperview.addSubview(newView)
        let index = addSuperview.subviews.index(of: newView)!        
        newView.translatesAutoresizingMaskIntoConstraints = false
        let heightConstraint = newView.widthAnchor.constraint(equalToConstant:addsubView.frame.size.width )
        let widthConstaint = newView.heightAnchor.constraint(equalToConstant:31 )
        let topConstraint = newView.topAnchor.constraint(equalTo: addSuperview.topAnchor, constant: space - 31)  NSLayoutConstraint.activate([heightConstraint,topConstraint,widthConstaint])

    }

委托更改父视图的高度

func txtcheck(text: String!) {
        print(text)
        for constraint in addSuperview.constraints {
            if constraint.firstAttribute == NSLayoutAttribute.height
            {
                constraint.constant -=  45
                // Here I have to set constraint for bottom view and topview of deleted view but I don't know how to do
            }
        }
    }

这里是link演示项目 https://github.com/logictrix/addFieldDemo

不是添加每个新的 AvalabileTimeView 都有自己的约束,而是使用 UIStackView - 您可以删除几乎所有用于添加/删除新视图的现有代码。

查看.addArrangedSubview().removeArrangedSubview()

这是一些示例代码...您需要在 Interface Builder 中添加一个 UIStackView,将其连接到 Outlet,并调整约束,仅此而已:

// in ViewController.swift

@IBOutlet weak var availableTimeStackView: UIStackView!

@IBAction func addMoreBnt(_ sender: UIButton) {

    // instantiate a new AvalabileTimeView
    let newView : AvalabileTimeView = AvalabileTimeView()

    // set its delegate to self
    newView.delegate = self as AvalabileTimeDelegate

    // add it to the Stack View
    availableTimeStackView.addArrangedSubview(newView)

    // standard for auto-layout
    newView.translatesAutoresizingMaskIntoConstraints = false

    // only constraint needed is Height (width and vertical spacing handled by the Stack View)
    newView.heightAnchor.constraint(equalToConstant: 31).isActive = true

}

// new delegate func
func removeMe(_ view: AvalabileTimeView) {

    // remove the AvalabileTimeView from the Stack View
    availableTimeStackView.removeArrangedSubview(view)

}

// in AvalabileTimeView.swift

protocol AvalabileTimeDelegate{
    // don't need this anymore
    func txtcheck(text: String!)

    // new delegate func
    func removeMe(_ view: AvalabileTimeView)
}

@IBAction func deletebnt(_ sender: UIButton) {
    delegate.removeMe(self)
}