swift - 动态更改键盘扩展的高度

标签 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()
}

对于为什么这不起作用的任何帮助和见解,我将不胜感激。谢谢

最佳答案

我找到了一个解决方案。 我在 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/

相关文章:

ios - 泛型 Obj-c 的 Swift 3 扩展无法访问类的泛型参数

linux - 系统配置键盘在 Fedora 上不工作

asp.net - 在 ASP.NET 文本框中捕获按键

postgresql - 如何使用触发器来防止 PostgreSQL 中的重复记录?

ios - 为什么向 UIImage 添加中心对齐约束会导致角半径设置失效?

ios - 从 Swift 函数中的异步调用返回数据

ios - 如何快速循环 UIBezierPath 中的点?

ios - 从 1.1 升级到 1.2 后出现奇怪的快速错误

android - 为什么当 View 聚焦时 RecyclerView 会滚动到 View 顶部

sql - 显示 Oracle SQL 中表的所有约束的名称