I know that this question has been asked over and over again, but nothing seems to be working for me. Most of the solutions around are pretty out of date, and the rest are incredibly huge blocks of code that are ten times larger then the actual projects coding. I have a few UITextFields lined up vertically, but when the keyboard launches to edit one, it covers up the text field. I was wondering if there is a simple beginner way to scroll the view up, and then back down when the editing starts and ends?
Thank you.
Since I found it, I use TPKeyboardAvoiding - https://github.com/michaeltyson/TPKeyboardAvoiding.
It is working great, and is very easy to setup:
UIScrollView
into your view controller's xibTPKeyboardAvoidingScrollView
(still in the xib, via the identity inspector)You can also create it programmatically, if you want.
There is a class for the same need inside a
UITableViewController
; it is only needed in case you support a version of iOS below 4.3.I spent sometime on this problem and gathered pieces code to create one final solution. My problem was related to UITableView scrolling and keyboard open/close.
You need two partial methods in your cell class:
and then in your view controller class:
If you have a
UITableView
or aUIScrollView
it's better to change values forcontentOffset
instead of making changes to theframe
.Working on Peter's Answer, adding this method to your class works nicely:
That's it, no need to add the
textViewDidEndEditing
method.I shouldn't need to say this, but for this to work your
UITextField
orUITextView
must be a delegate of your controller.@BenLu and other users who are facing problem of the function are never getting called is because of following reason: As the delegate inbuild function bydefaults return void instead of BOOL this is how it should be as follows:
i have a scrollview and 3 text fields in this. I have a simple code from my own application :
.h file is :
.m file :
Starting with Peter's answer, I developed the following approach in Swift 3.0 under iOS 10.1. I'm doing this for a textView, so I have implemented the UITextViewDelegate functions textViewDidBeginEditing and textViewDidEndEditing where I adjust the view's bounds. As you can see, I set the origin Y value to a small positive number to scroll up and then back to 0 to return to the original position.
Here is the relevant code from my ViewController. You don't need to animate, but it adds a nice touch.