Search code examples
iosswiftuikitkeyboard-events

iOS built in solution for managing keyboard pushes or overlaps to UI elements


Suppose an iOS app that we need to handle the way the keyboard interacts with the UI. Specifically, how to manage whether the keyboard pushes views (e.g., a button at the bottom) or overlaps them.

Our Question: does iOS provide any built-in solution, like Android’s windowSoftInputMode, to decide if elements are pushed by the keyboard or overlapped, without manually managing constraints or listening to keyboard notifications?

What we have tried:

Observing UIResponder.keyboardWillShowNotification and manually updating the constraints of UI elements.


Solution

  • The closest thing to automatic keyboard avoidance that iOS supplies is the keyboardLayoutGuide. If your interface bottom is pinned to the top of the keyboardLayoutGuide, it will avoid the keyboard automatically and you won't have to listen for any notifications.

    If you don't want something to avoid the keyboard then just don't pin it to the keyboardLayoutGuide in the first place. If you want something to avoid the keyboard sometimes and not at other times, you'll need to take advantage of the fact that constraints can be disabled/enabled.