我有一个用户可以花费和收缩的结果容器。我想删除一个约束并添加一个新约束。单击它可以正常工作但第二次单击(即设置newConstraint.active = false并且resultTopConstraint = true会导致崩溃)。我有以下内容:
@IBAction func toggleResultContainer(sender: AnyObject) {
isResultsOpen = !isResultsOpen
//resultTopConstraint.constant = isResultsOpen ? -300.0 : 0.0
self.view.sendSubviewToBack(searchView)
let newConstraint = NSLayoutConstraint(
item: resultsContainer,
attribute: .Top,
relatedBy: .Equal,
toItem: resultsContainer.superview!,
attribute: .Top,
multiplier: 1.0,
constant: 30.0
)
if(isResultsOpen){
resultTopConstraint.active = false
newConstraint.active = true
}else{
resultTopConstraint.active = true
newConstraint.active = false
}
UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 10.0, options: .CurveEaseIn, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
并获取Unable to simultaneously satisfy constraints.
上述代码是否有效,这是否同时满足约束问题?我试过设置约束
@IBOutlet var resultTopConstraint: NSLayoutConstraint!
同时weak
和strong
(每https://stackoverflow.com/a/28717185/152825)但似乎没有效果。甲
答案 0 :(得分:1)
试试这个:
if (isResultsOpen) {
NSLayoutConstraint.deactivateConstraints([resultTopConstraint])
NSLayoutConstraint.activateConstraints([newConstraint])
} else {
NSLayoutConstraint.deactivateConstraints([newConstraint])
NSLayoutConstraint.activateConstraints([resultTopConstraint])
}