Swift 动态更改键盘扩展的高度
我有一个键盘扩展,它以高度常数280开始。我用下面的代码给它高度限制。我称之为在视图中显示Swift 动态更改键盘扩展的高度,swift,keyboard,constraints,ios9,Swift,Keyboard,Constraints,Ios9,我有一个键盘扩展,它以高度常数280开始。我用下面的代码给它高度限制。我称之为在视图中显示 func normalHeight() { constant = 280 UIView.animateWithDuration(1.0) { let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, at
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()
}
我将感谢任何帮助和洞察为什么这不起作用。谢谢,我找到了解决办法。
我在视图中添加了两个高度约束,并为它们指定了高优先级和低优先级。
约束优先级应该是高优先级和低优先级,分别为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()
}
为了恢复到正常高度,我打电话给
func extendedHeight() {
bheightConstraint.priority = 750
aheightConstraint.priority = 250
view.setNeedsLayout()
}
func normalHeight() {
bheightConstraint.priority = 250
aheightConstraint.priority = 750
view.setNeedsLayout()
}