关于如何改变自定义键盘高度,有几个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!
}
这没有用,输出警告仍然存在。我该如何解决这个问题呢?具体来说,怎么做才能消除输出错误信息?
尝试设置heightConstraint优先级小于1000。一些事情,如打击 。
heightConstraint.priority = 990
试试将高度限制优先级设置为小于1000.
heightConstraint.priority = 999
这个警告在iOS 13中仍然是一个问题。
出现这个问题是因为键盘扩展的视图(KeyboardViewController
’s view
属性)将自动调整遮罩转化为约束。
你想要的是通过调用自动生成的高度约束来移除。
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
}
}
你甚至可以删除高度约束,让子视图约束决定键盘的高度。