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

问题描述:

我有一个视图,我在编程上通过xib添加按钮来创建另一个视图。我能够创建多个视图点击添加更多按钮和删除也工作,如果我删除最后一个视图,但问题发生在中间视图由于缺少约束查看不正确更新波纹管图像它是如何看待如何删除视图并更新约束?

在开始查看这个样子

enter image description here

添加更多视图后

enter image description here

除去中间视图后

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

    } 

委托来改变上海华

的高度0
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

+0

使用UITabelView。您可以非常轻松地实现此类型的操作。 – Ujesh

+0

@Ujesh是的,如果我没有解决这个问题,我必须使用其他选项,现在我正试图解决这个问题。 –

+0

这当然可以修复,但最好使用TableView,如果这是您无法使用TableView的分配,这是可以理解的。否则,请始终使用Apple为您提供的最佳工具。 – CoderFrom94

而是有自己的约束添加每个新AvalabileTimeView的,使用UIStackView - 你可以删除几乎所有的现有代码的添加/删除新的看法。

.addArrangedSubview().removeArrangedSubview()

下面是一些示例代码...你需要在Interface Builder添加UIStackView,它连接到电源插座,并调整限制,但是这就是全部:

// 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) 
} 
+0

感谢您记住我关于堆栈视图我完全忘记了这可能会解决我的问题我会检查并让你知道 –