Moving content around when the iOS keyboard shows using UIKeyboardWillChangeFrame

Simon B
Simon B
Nov 7 · 2 min read

Picture this: you’ve gone and built yourself a beautiful screen for your iOS that has a text field. It looks great. Then… the keyboard shows up and it blocks everything!

“OK,” you say. “No problem. I’ll just copy-paste some code from StackOverflow or a blog post…”

All is well. The view updates when the keyboard shows. Done!


— but then your user complains that the content is getting covered by the keyboard? How could that be!?

Enter custom keyboards. When the custom keyboard it’s the Wild West. The keyboard height changes without warning! Keyboard accessory views! What is a developer to do?

Well, I had this problem too. And I quickly discovered a solution that not only fixed it but at the same time it simplified the keyboard handling process:

keyboardWillChangeFrame to the rescue!

Where’s willHide you ask? You don’t need it anymore. If all you want to do is move content when the keyboard shows and hides, why do you care which of the two is happening? All you care is where that top of the keyboard is. And with willChangeFrame you can just get that information and be totally agnostic about whether the keyboard is showing or not.

Use keyboardWillShow and keyboardWillHide if you want to update your view the first time the keyboard shows and when it eventually hides regardless of its size.

You can also use didChangeFrame if for whatever reason you want your changes to happen after the keyboard change updates.

Keyboard sizesin iOS can be a bit… unpredictable…
Keyboard sizesin iOS can be a bit… unpredictable…
Keyboard heights in iOS can be a bit… unpredictable…
Simon B

Written by

Simon B

Senior Software Engineer at Swift Medical in Toronto

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade