I have an app where, in Interface Builder, I set up a UIView
that has a text field near the bottom of the view. When I run the app and try to enter text into that field, the keyboard slides up overtop of the field so I can't see what I'm typing until I hide the keyboard again.
Has anyone else run into this problem and found a good way to solve it without either making the parent view scrollable or moving the text field farther up the screen?
I believe on newer versions of iOS (6.1+, possibly even earlier), the underlying view, at least for UITableView, auto-shrinks when the keyboard pops up. So you only need to make the text field visible in that view. In
init
:then:
IQKeyboardManager do this for you with NO LINE OF CODE, only need to drag and drop related source file to project. IQKeyboardManager also support Device Orientation, Automatic UIToolbar Management, keyboardDistanceFromTextField and much more than you think.
Here is the Control Flow Chart:
Step1:- Added global notifications of
UITextField
,UITextView
, andUIKeyboard
in a singleton class. I called it IQKeyboardManager.Step2:- If found
UIKeyboardWillShowNotification
,UITextFieldTextDidBeginEditingNotification
orUITextViewTextDidBeginEditingNotification
notifications, then try to gettopMostViewController
instance from theUIWindow.rootViewController
hierarchy. In order to properly uncoverUITextField
/UITextView
on it,topMostViewController.view
's frame needs to be adjusted.Step3:- Calculated expected move distance of
topMostViewController.view
with respect to first respondedUITextField
/UITextView
.Step4:- Moved
topMostViewController.view.frame
up/down according to the expected move distance.Step5:- If found
UIKeyboardWillHideNotification
,UITextFieldTextDidEndEditingNotification
orUITextViewTextDidEndEditingNotification
notification, then again try to gettopMostViewController
instance from theUIWindow.rootViewController
hierarchy.Step6:- Calculated disturbed distance of
topMostViewController.view
which needs to be restored to it's original position.Step7:- Restored
topMostViewController.view.frame
down according to the disturbed distance.Step8:- Instantiated singleton IQKeyboardManager class instance on app load, so every
UITextField
/UITextView
in the app will adjust automatically according to the expected move distance.That's all