カスタム キーボードの高さを変更する方法については、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!
}
これは役に立ちませんでした。出力警告はまだ残っています。これを解決するにはどうすればよいですか?具体的には、出力エラー メッセージを取り除くにはどうすればよいですか?