2015-02-11 83 views
1

我正在玩自動佈局一點,我有按鈕,只是彈出列表中選擇。問題是,我添加了一個開關,可以讓我切換/關閉searchBar是否應該出現在彈出窗口中。清除所有垂直自動佈局約束

enter image description here enter image description here enter image description here

所以我需要重新設置導向頭,並根據切換搜索欄的表約束。

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    self.view.setTranslatesAutoresizingMaskIntoConstraints(false) 
    if (searchBar.hidden) { 
     let views = ["tableView": tableView] 
     constraintsWithoutSearchbar = NSLayoutConstraint.constraintsWithVisualFormat("V:|[tableView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) 
     self.view.removeConstraints(constraintsWithSearchbar) 
     self.view.addConstraints(constraintsWithoutSearchbar) 
    } else { 
     let views = ["tableView": tableView, "searchBar": searchBar] 
     constraintsWithSearchbar = NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[searchBar][tableView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) 
     self.view.removeConstraints(constraintsWithoutSearchbar) 
     self.view.addConstraints(constraintsWithSearchbar) 
    } 
    tableView.reloadData() 
} 

的問題是,我必須存儲關閉這些限制將其刪除,並添加新的,否則我得到無法同時滿足約束條件:

我得到了它與此代碼的酥料餅的工作。錯誤。我也從IB獲得了默認的自動生成約束的這個錯誤。

有沒有一種方法可以在新的之前擦除所有垂直約束?

回答