我有一个键盘扩展,它以 280 的高度常量开始。我用下面的代码给它高度限制。我在 viewWillAppear 中调用它。
func normalHeight() {
constant = 280
UIView.animateWithDuration(1.0) {
let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.constant)
heightConstraint.priority = self.normalHeightPriority
self.view.addConstraint(heightConstraint)
}
self.view.setNeedsUpdateConstraints()
self.view.setNeedsDisplay()
}
我还使用下面的代码对高度限制的增加进行动画处理,该代码也有效。高度增加。
func extendedHeight() {
constant = 400
UIView.animateWithDuration(1.0) {
let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.constant)
heightConstraint.priority = self.extendedHeightPriority
self.view.addConstraint(heightConstraint)
}
self.view.setNeedsUpdateConstraints()
self.view.setNeedsDisplay()
}
要恢复高度,我再次通过按钮调用 normalHeight(),但这不起作用。我想也许优先级是问题所在,因为 extendeHeight 约束的优先级为 1000,而 normalHeight 约束的优先级为 999。 因此,当函数被调用时,我会翻转优先级,如下所示。仍然没有骰子。
func normalHeight() {
constant = 280
normalPriority = 1000
extendedPriority = 999
UIView.animateWithDuration(1.0) {
let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.constant)
heightConstraint.priority = self.normalPriority
self.view.addConstraint(heightConstraint)
}
print("Normal Height Called")
self.view.setNeedsUpdateConstraints()
self.view.setNeedsDisplay()
}
像这样扩展优先级
func extendedHeight() {
constant = 400
extendedPriority = 1000
normalPriority = 999
UIView.animateWithDuration(1.0) {
let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.constant)
heightConstraint.priority = self.extendedPriority
self.view.addConstraint(heightConstraint)
}
self.view.setNeedsUpdateConstraints()
self.view.setNeedsDisplay()
}
对于为什么这不起作用的任何帮助和见解,我将不胜感激。谢谢
最佳答案
我找到了一个解决方案。 我在 View 中添加了两个高度限制,并赋予它们高低优先级。 约束优先级应该是高优先级和低优先级,分别是 750 和 250 而不是我之前使用的 1000 和 999
现在,当我想更改高度时,我会翻转两个已安装约束的优先级。
func initialHeight() {
AConstant = 280
BConstant = 400
self.aheightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.AConstant)
self.aheightConstraint!.priority = 750
self.view.addConstraint(self.aheightConstraint!)
self.bheightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: self.BConstant)
self.bheightConstraint.priority = 250
self.view.addConstraint(bheightConstraint)
self.view.setNeedsDisplay()
对于加长的高度我叫
func extendedHeight() {
bheightConstraint.priority = 750
aheightConstraint.priority = 250
view.setNeedsLayout()
}
为了恢复正常高度我打电话
func normalHeight() {
bheightConstraint.priority = 250
aheightConstraint.priority = 750
view.setNeedsLayout()
}
关于swift - 动态更改键盘扩展的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39232518/