I have an app that has a text field on the lower half of the view. This means that when I go to type in the text field the keyboard covers the textfield.
How would I go about moving the view upwards while typing so I can see what i'm typing and then moving it back down to its original place when the keyboard disappears?
I've looked everywhere but all the solutions appear to be in Obj-C which I can't quite convert just yet.
Any help would be greatly appreciated.
I see all answers are moving the view itself by the value of the keyboard height. Well, I have an elaborate answer, which could be useful if you are using constraints i.e
autolayout
, that moves a view by changing its constraint value (bottom or top constraints for example) by a predefined value or you can use keyboard size value.In this example, I use bottom constraint from the textfield to Bottom Layout View with initial value of 175.
My two cents for beginners: in above samples someone changes coordinates, other uses "autoresizing mask", and other constraints:
As Apple says, do not mix these 3 types of logic. If You have constraints in Storyboard, do not try to change x/y. It definitively not work.
If you have 2 or more text fields on the same VC, and the user taps on one of them and then taps on the other one, without calling the function keyboardWillHide, the view is going upwards one more time, which is not necessary, because you'll have the keyboard, a blank space which has the height of the keyboard, and then the view, using the code in the answer I edited:
To solve this, replace the two functions "KeyboardWillShow/Hide" to these:
For Swift 3, I made a UIViewController subclass since I needed constant behavior in all View Controllers.
if you are like me who has tried all the above solutions and still your problem is not solved, I have a got a great solution for you that works like a charm. First I want clarify few things about some of solutions mentioned above.