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.
Here is my solution (actually this code is for the case when you have few textfields in your view, this works also for the case when you have one textfield)
i've read answers and solved my problem by this lines of code:
I have a few UITextFields and want the view to move up differently depending on which textField is tapped.
I noticed that the other answers involved cutting some of the top from the view. If you want to simply resize the view without cutting any content, just try this method :)
The validated answer doesn't take in account the textfield position and has some bug (double displacement, never come back the primary position, displacement even if the texfield is on top of the view...)
The idea is :
then we will be able to move the view only if necessary and of the specific displacement in oder to have the focused texField just over the keyboard
Here is the code :
Swift 4
}
Swift 3 code
One of the popular answers on this thread uses the following code:
There's an obvious problem with offsetting your view by a static amount. It'll look nice on one device but will look bad on any other size configuration. You'll need to get the keyboards height and use that as your offset value.
Here's a solution that works on all devices and handles the edge-case where the user hides the predictive text field while typing.
Solution
Important to note below, we're passing self.view.window in as our object parameter. This will provide us with data from our Keyboard, such as its height!
We'll make it look nice on all devices and handle the case where the user adds or removes the predictive text field.
Remove Observers
Don't forget to remove your observers before you leave the view to prevent unnecessary messages from being transmitted.
Update based on question from comments:
If you have two or more text-fields, you can check to see if your view.frame.origin.y is at zero.