keyboard hides text fields

This content has 10 years. Please, read this page keeping its age in your mind.

The situation is like that:

You have a UIViewController which has many text fields. It could be like:

iOS Simulator Screen shot Jul 26, 2014, 14.04.17

The problem is that when the keyboard appears, it hides the text fields. The solution is to lay down your text fields in a UIScrollView and write the appropriate code to move it accordingly.

If you have tried, like me, to use keyboard notification using UIKeyboardWillShowNotification and UIKeyboardWillHideNotification and implementing UITextFieldDelegate delegate methods soon you will find yourself playing with content offsets and trying to fine running and smooth the movement.

On the other hand, Mr Micheal Tyson has developed a neat solution, the TPKeayboardAvoiding. It’s a subclass of UIScrollView. When the keyboard is about to appear, the subclass will find the subview that’s about to be edited, and adjust its frame and content offset to make sure that view is visible, with an animation to match the keyboard pop-up. When the keyboard disappears, it restores its prior size.

The usage? Very simple, it is a drop-in universal solution.
In your Xcode project, you just have to add 4 files: UIScrollView+TPKeayboardAvoidingAditions.h and m as well as TPKeyboardAvoidingScrollView.h and m.

Then select the your UIScrollView and from the Identity Inspector change its class to TPKeyboardAvoidingScrollView. I assume that you have already done all the other necessary adjustments like setting the UIViewController delegate of the ScrollView or the correct auto layout arrangements between the UIScrollView and the other elements.

Hint: This is a very simple yet educational video to understand the essentials between UIScrollView and auto layout in iOS7.

1. A drop-in universal solution for moving text fields out of the way of the keyboard
2. michaeltyson/TPKeyboardAvoiding from GitHub