3

关于如何更改自定义键盘高度有几个 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!
}

这没有帮助,输出警告仍然存在。我该如何解决这个问题?具体来说,我该怎么做才能摆脱输出错误消息?

4

2 回答 2

3

尝试将 heightConstraint 优先级设置为小于 1000。例如 blow :

heightConstraint.priority = 990

或者

heightConstraint.priority = 999
于 2014-10-27T13:36:28.483 回答
1

这个警告在 iOS 13 中仍然是一个问题。

它出现是因为键盘扩展(KeyboardViewControllerview属性)的视图将自动调整掩码转换为约束。

您想要的是通过调用删除自动生成的高度约束:

view.translatesAutoresizingMaskIntoConstraints = false

然而,这还不够:您必须替换重要的生成约束。

我在viewWillAppear函数中这样做了,因为此时存在父视图。我使用该constraintHaveBeenAdded属性来避免多次添加相同的约束,因为该函数可以重复调用。

class KeyboardViewController: UIInputViewController {

  private var constraintsHaveBeenAdded = false

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    initKeyboardConstraints()
  }

  private func initKeyboardConstraints() {
    if constraintsHaveBeenAdded { return }
    guard let superview = view.superview else { return }
    view.translatesAutoresizingMaskIntoConstraints = false
    view.leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true
    view.bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
    view.rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true
    view.heightAnchor.constraint(equalToConstant: 250.0).isActive = true
    constraintsHaveBeenAdded = true
  }

}

您甚至可以删除高度约束,让子视图约束决定键盘高度。

于 2020-05-31T10:21:52.307 回答