2 Minute Video Tutorial: Let Auto Layout and Constraints Move View for Keyboard

There are various ways to move a view to accommodate the keyboard when it opens.

In this 2 minute video tutorial, I demonstrate one method that I really like, which is to let Auto Layout and Constraints do the work.

The code is available on GitHub.

This looks best in Full Screen 1080p.

Note: In the video, I had a view.setNeedsLayout() after changing the constraint constant. This is not needed, since the constraint change effectively sets the view as needing a layout already. I’ve removed it in the GitHub version.

You can also see a written description of this technique for moving the keyboard and using constraints in code here in a more recent tutorial.

I’ve recently updated the code in GitHub to be Swift 3 compliant. Also remember that if your keyboard doesn’t open in a simulator, make sure you have toggled the software keyboard ON in the simulator.

Leave a Comment: