我有一个UIView的子类,我想操纵一个约束,但它不起作用。
当按下按钮时,交换ViewActivated更改并调用函数。
func setUpLayout() {
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive = true
bottomContainer.leadingAnchor.constraint(equalTo: scrollViewContrainer.leadingAnchor).isActive = true
bottomContainer.trailingAnchor.constraint(equalTo: scrollViewContrainer.trailingAnchor).isActive = true
bottomContainer.topAnchor.constraint(equalTo: configBar.bottomAnchor).isActive = true
bottomContainer.bottomAnchor.constraint(equalTo: scrollViewContrainer.bottomAnchor).isActive = true
}
设置子视图和translatesAutoresizingMaskIntoConstraints。
func setExchangesView() {
bottomContainer.bottomAnchor.constraint(equalTo: scrollViewContrainer.bottomAnchor).isActive = false
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive = false
bottomContainer.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
}
现在我想通过调用这个函数来操纵约束:
{{1}}
但是这个约束仍然被激活:
bottomContainer.heightAnchor.constraint(equalToConstant:500).isActive
我错过了什么吗?将约束设置为false以停用它是不够的?我还需要打电话吗?
答案 0 :(得分:1)
这不会停用旧约束,因为它会停用新创建的约束,所以
var heightCon:NSLayoutConstraint!
var botCon:NSLayoutConstraint!
//
heightCon = bottomContainer.heightAnchor.constraint(equalToConstant: 500)
heightCon.isActive = true
botCon = bottomContainer.bottomAnchor.constraint(equalTo: scrollViewContrainer.bottomAnchor)
botCon.isActive = true
然后,您可以轻松引用约束并停用它们并添加新约束
答案 1 :(得分:1)
setUpLayout()
方法时, setUpLayout()
都会添加新约束。您必须保存约束引用并在下次更新它。
例如。
// class variable
var bottomConstraint:NSLayoutConstraint? = nil
bottomConstraint = bottomContainer.heightAnchor.constraint(equalToConstant: 500)
bottomConstraint.isActive = true
以后更新约束
bottomConstraint.constant = 100
bottomConstraint.isActive = true