以编程方式更新约束?
Updating Constraints programmatically?
我有一个 UIView 的子类,我想操作一个约束但它不起作用。
按下按钮时 exchangesViewActivated 发生变化并调用函数。
var exchangesViewActivated = false {
didSet {
if exchangesViewActivated == true {
setExchangesView()
} else {
setUpLayout()
}
}
}
Die 子视图和 translatesAutoresizingMaskIntoConstraints 已设置。
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
}
现在我想通过调用这个函数来操纵约束:
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
}
但此约束保持激活状态:
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive
我错过了什么吗?将约束设置为 false 以停用它还不够吗?我必须打电话给其他人吗?
这不会停用旧的约束,因为它会停用新创建的约束
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
然后您可以轻松引用约束并停用它们并添加新约束
setUpLayout()
会在您每次调用 setUpLayout()
方法时添加新的约束。您必须保存约束参考并在下次更新它。
例如
// class variable
var bottomConstraint:NSLayoutConstraint? = nil
bottomConstraint = bottomContainer.heightAnchor.constraint(equalToConstant: 500)
bottomConstraint.isActive = true
稍后更新约束
bottomConstraint.constant = 100
bottomConstraint.isActive = true
我有一个 UIView 的子类,我想操作一个约束但它不起作用。
按下按钮时 exchangesViewActivated 发生变化并调用函数。
var exchangesViewActivated = false {
didSet {
if exchangesViewActivated == true {
setExchangesView()
} else {
setUpLayout()
}
}
}
Die 子视图和 translatesAutoresizingMaskIntoConstraints 已设置。
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
}
现在我想通过调用这个函数来操纵约束:
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
}
但此约束保持激活状态:
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive
我错过了什么吗?将约束设置为 false 以停用它还不够吗?我必须打电话给其他人吗?
这不会停用旧的约束,因为它会停用新创建的约束
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
然后您可以轻松引用约束并停用它们并添加新约束
setUpLayout()
会在您每次调用 setUpLayout()
方法时添加新的约束。您必须保存约束参考并在下次更新它。
例如
// class variable
var bottomConstraint:NSLayoutConstraint? = nil
bottomConstraint = bottomContainer.heightAnchor.constraint(equalToConstant: 500)
bottomConstraint.isActive = true
稍后更新约束
bottomConstraint.constant = 100
bottomConstraint.isActive = true