iOS 对自定义键盘设置了必需的高度限制

iOS puts a required height constraint on custom keyboard

我正在尝试为 UITextField 创建自定义键盘。这不是系统扩展;它仅在我的应用程序中。

myTextField.inputView = CustomKeyboard()

CustomKeyboard class 具有子视图和自动布局约束,需要比通常的系统键盘高一点。这会导致错误,因为系统正在添加必需的 customKeyboard.height == 216 约束。

如何告诉它不要添加此约束?它被添加到 CustomKeyboardsuperview.

中的约束数组中
(lldb) po type(of: self)
MyApp.CustomKeyboard    
(lldb) po self.superview!.constraints[0]
<NSLayoutConstraint:0x17428ab40 MyApp.CustomKeyboard:0x100d37580.height == 216   (active)>

很遗憾,您没有显示自定义键盘代码。但很可能这里的问题是它不是 UIInputView 的子类。使它成为 UIInputView 子类,现在你可以设置它的框架;原点和宽度将被忽略,但高度将被遵守。请确保您的 UIInputView 的内部约束与此高度不冲突。

我从 Apple 开发者支持那里得到了答复。我已经实施了这个并且它有效。他们是这样说的:

If your inputView is using Auto Layout and has enough constraints such that it can determine its own height (which is the case in your example project), then all you need to do is override intrinsicContentSize in your input view and return a CGSize with a height that is not UIViewNoIntrinsicMetric. For example, this will do:

override var intrinsicContentSize: CGSize {
    return CGSize(width: UIViewNoIntrinsicMetric, height: 0)
}