动态更改键盘扩展的高度

时间:2016-08-30 16:18:39

标签: swift keyboard constraints ios9

我有一个键盘扩展名,高度常量为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()
}

我将不胜感激任何帮助,并深入了解为什么这不起作用。感谢

1 个答案:

答案 0 :(得分:2)

I found a solution. I added two height constraints to the view and gave them priorities of high and low. The constraint priorities should be high and low priority which is 750 and 250 respectively instead of 1000 and 999 I was using earlier

Now when I want to change the height I flip the priorities of the two installed constraints.

    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()

For the extended height I call

 func extendedHeight() {
    bheightConstraint.priority = 750
    aheightConstraint.priority = 250
    view.setNeedsLayout()

}

and to revert to normal height I call

    func normalHeight() {
    bheightConstraint.priority = 250
    aheightConstraint.priority = 750
    view.setNeedsLayout()
}