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 have face the same issue in
UITableView
textField cells. I solve this issue by implementing following method to listen the keyboard notification.Observer for the notifications here:
Handle those notification by using below function:
Check this out. No hassle for you.
This solution is very neat. All you have to do is to add your textfields in a
UIScrollView
and change its class toTPKeyboardAvoidingScollView
, if you are using storyboards. The scroll view is extended in such a way that it would detect when keyboard is visible and will move itself above keyboard at a reasonable distance. It is perfect solution because its independent of yourUIViewController
. Every necessary thing is done within the the above mentioned class. Thanks Michael Tyson et all.TPKeyboardAvoiding
I guess one way would be to move your whole views position from (x,y) to (x,y-keybaardHeight) when the textfield is clicked and put it back when the keyboard is dismissed , might look a little odd as the view just comes up (maybe it wouldnt be bad if you animate it).
Just slide the view up and down as needed :
Don't forget to set
self
as aUITextFieldDelegate
and as the actual textFielddelegate
.(Thanks to Ammagrammer, this is just a shorter answer using blocks for animations)
I have something else if you want. The point here is that you want to set the center your UIView on the text field you are editing.
Before that, you have to save your INITIAL_CENTER, as a CGPoint, from self.view.center and your INITIAL_VIEW as a CGRect from self.view.frame in a const property.
You can create a method like this :
Then, on your UITextFieldDelegate, you have to call centerOn:(CGRect) in following methods :
textFieldDidBeginEditing:(UITextField*) with, as a parameter, the frame of the text field you want to center on.
And you have to call it in your event handler, where you close your keyboard,
textFieldDidEndEditing:(UITextField*) can be one of the ways to do it, putting the INITIAL_VIEW as a parameter of centerOn:(CGRect).
How about the official solution: Moving Content That Is Located Under the Keyboard