关于如何更改自定义键盘高度有几个 SO 答案。其中一些在这里工作,但那些工作导致约束冲突错误打印到控制台输出:
Unable to simultaneously satisfy constraints...
Will attempt to recover by breaking constraint...
这是我设置自定义键盘高度的非常简单的键盘控制器(是 Swift):
class KeyboardViewController: UIInputViewController {
private var heightConstraint: NSLayoutConstraint?
private var dummyView: UIView = UIView()
override func updateViewConstraints() {
super.updateViewConstraints()
if self.view.frame.size.width == 0 || self.view.frame.size.height == 0 || heightConstraint == nil {
return
}
inputView.removeConstraint(heightConstraint!)
heightConstraint!.constant = UIInterfaceOrientationIsLandscape(self.interfaceOrientation) ? 180 : 200
inputView.addConstraint(heightConstraint!)
}
override func viewDidLoad() {
super.viewDidLoad()
self.dummyView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(self.dummyView)
view.addConstraint(NSLayoutConstraint(item: self.dummyView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: self.dummyView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0))
heightConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 0.0)
}
}
这会产生恼人的输出错误,heightConstraint
即我添加的updateViewConstraints
约束与标识为的约束冲突UIView-Encapsulated-Layout-Height
。
我试图删除冲突的常量 ( UIView-Encapsulated-Layout-Height
),updateViewConstraints
如下所示:
let defaultHeightConst = inputView.constraints().filter() {c in (c as? NSLayoutConstraint)?.identifier == "UIView-Encapsulated-Layout-Height"}.first as? NSLayoutConstraint
if defaultHeightConst != nil {
inputView.removeConstraint(defaultHeightConst!
}
这没有帮助,输出警告仍然存在。我该如何解决这个问题?具体来说,我该怎么做才能摆脱输出错误消息?