Rob N
Rob N

Reputation: 16399

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)>

Upvotes: 2

Views: 830

Answers (2)

Rob N
Rob N

Reputation: 16399

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)
}

Upvotes: 9

matt
matt

Reputation: 534885

Unfortunately you did not show your CustomKeyboard code. But in all probability the issue here is that it is not a UIInputView subclass. Make it a UIInputView subclass and now you can set its frame; the origin and width will be ignored, but the height will be obeyed. Make sure that your UIInputView's internal constraints do not conflict with this height.

Upvotes: 1

Related Questions