2017-06-15 35 views
0

我有一個視圖,我在編程上通過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

回答

3

而是有自己的約束添加每個新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

感謝您記住我關於堆棧視圖我完全忘記了這可能會解決我的問題我會檢查並讓你知道 –