Rob N Rob N -4 years ago 170
iOS Question

iOS puts a required height constraint on custom keyboard

I'm trying to create a custom keyboard for a

UITextField
. This is not a system extension; it's within my app only.

myTextField.inputView = CustomKeyboard()


The
CustomKeyboard
class has subviews and auto layout constraints that require a bit more height than the usual system keyboard. This causes an error because the system is adding a required
customKeyboard.height == 216
constraint.

How do I tell it not to add this constraint? It's added to the array of constraints in the
CustomKeyboard
's
superview
.

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

Answer Source

I got an answer from Apple developer support. I've implemented this and it works. Here's what they said:

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)
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download