Must a custom UIInputView be assigned directly to the UIInputViewController.inputView or can it be a subview?

I'm making a custom keyboard extension in Objective-C for iOS 15+.

Originally I designed myCustomInputView and put all my keyboard stuff into it, using constraints for everything. I then would add it as a subview like this: [myCustomKeyboardVC.inputView addSubview:myCustomInputView] and then contain it to .inputView.

That mostly worked fine, except that after making and dismissing several I had a bunch of myCustomInputView objects remaining, being referenced by _inputViewContent which isn't being used by a custom keyboard anymore.

I saw a suggestion that I should do this instead: myCustomKeyboardVC.inputView = myCustomInputView

so that myCustomInputView is assigned directly to the VC's .inputView rather than being a subview of it.

But this has problems because I now don't know what to constrain it to, and it runs wider than the screen. (ie, how do I constrain myCustomInputView to the keyboard width)?

So I guess my basic question is: must custom UIInputViews be assigned directly to .inputView or can they be a subview of it?

Accepted Answer

I found that the answer is "yes": your custom UIInputView object should be assigned directly to your custom UIInputViewController.inputView property.

Make sure that your custom UIInputView has translatesAutoresizingMaskIntoConstraints == YES. iOS will automatically size the keyboard area (ie, .inputView) as the device rotates, etc.

You can create a UILayoutGuide and add it to your custom UIInputView (with constraints to each edge), then have all of the subviews setup constraints to the layout guide.

I'll also note that assigning my custom UIInputView directly to .inputView also made this warning go away: -[UIInputViewController needsInputModeSwitchKey] was called before a connection was established to the host application. This will produce an inaccurate result. Please make sure to call this after your primary view controller has been initialized.

I hope this helps someone.

Must a custom UIInputView be assigned directly to the UIInputViewController.inputView or can it be a subview?
 
 
Q