Full source code for the project: https://github.com/yichizhang/CustomHeightKeyboard.git



I’ve been building an iOS keyboard app: AGK Ancient Greek Keyboard (Polytonic Greek)

In the app, I would like to have custom height for the keyboard, and the height of it changes depending on the orientation of the device (or depending the size of the screen, since from iOS 8, [UIScreen bounds] is interface-oriented).

It should be possible because in Apple’s App Extension Programming Guide: Custom Keyboard, there is a note on keyboard height:

Note

In iOS 8.0, you can adjust a custom keyboard’s height any time after its primary view initially draws on screen.

The solution (works for both iOS 8 and iOS 9)

I found a solution in Stackoverflow. It’s skyline75489’s answer in iOS 8 Custom Keyboard: Changing the Height.

It’s basically adding an extra height constraint to inputView or view (NB: they are identical) in updateViewConstraints:.

It works perfectly in iOS 8, but not iOS 9. I figured out a workaround for iOS 9. It’s simple - just move ‘next keyboard button’ constraints set up code to viewDidAppear:.

Full source code: https://github.com/yichizhang/CustomHeightKeyboard.git

Conclusion

I’m not entirely sure how it works. I’ve tried putting methods like setNeedsUpdateConstraints and layoutIfNeeded in different places and it didn’t work.

If you know why, please do not hesitate to leave a comment.