ios - 如何删除 View 和更新约束?

标签 ios swift swift3 nslayoutconstraint

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

在开始 View 看起来像这样

enter image description here

添加更多 View 后

enter image description here

移除中间 View 后

enter image description here

删除按钮代码

@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])

    }

委托(delegate)改变父 View 的高度

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
            }
        }
    }

这是演示项目的链接 https://github.com/logictrix/addFieldDemo

最佳答案

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

查看.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)
}

关于ios - 如何删除 View 和更新约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44567504/

相关文章:

ios - 调用 didEnterRegion 时,didUpdateLocations 不再被调用

ios - CoreData : warning: Unable to load class named 'x' for entity 'x' . 找不到类,改用默认的 NSManagedObject

ios - 多次调用observeValueForKeyPath

swift - 迭代 API 的结果并知道它何时完成的最佳方法是什么?

ios - AudioKit.renderToFile AKClipPlayer 同步问题。渲染文件与第一个剪辑位置不同步并且也被截断

ios - Swift 代码应用程序在使用分发证书启动时崩溃

macos - 如何将自定义表格单元格 View 放入 NSTableView 中?

ios - Firebase .childAdded 观察者复制 child

swift - 使用未声明的类型错误 : How to compare object with class type?

ios - 需要帮助声明数组以在 Swift 3 中检索 plist 信息