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 made a cocoapod to simplify the matter:
https://github.com/xtrinch/KeyboardLayoutHelper
How to use it:
Make an auto layout bottom constraint, give it a class of KeyboardLayoutConstraint in module KeyboardLayoutHelper and the pod will do the work necessary to increase it to accomodate appearing and disappearing keyboard. See example project on examples how to use it (I made two: textFields inside a scrollView, and vertically centered textFields with two basic views - login & register).
The bottom layout constraint can be of the container view, the textField itself, anything, you name it.
Add this to your viewcontroller. Works like a charm. Just adjust the values.
Easiest way that doesn't even require any code:
The object will auto-move up with the keyboard, in sync.
This feature shud have come built in Ios, however we need to do externally.
Insert the below code
* To move view when textField is under keyboard,
* Not to move view when textField is above keyboard
* To move View based on the height of the keyboard when needed.
This works and tested in all cases.
|::| Sometimes View wil be down, In that case use height +/- 150 :
Updated for Swift 3...
As others have said, you need to add notification observers in your controller's viewDidLoad() method, like so:
Remember to remove your observers where appropriate (I do it in the viewWillDisappear() method)
Then, implement your show and hide methods - notice the line that tells the app to ignore interaction events (beginIgnoringInteractionEvents). This is important since without it, the user could tap on a field or even a scrollview and cause the shift to occur a second time, resulting in a terrible UI glitch. Ignoring interaction events prior to the keyboard showing and hiding will prevent this:
Lastly, re-enable user interactions (remember, this method fires after the keyboard didShow or didHide):
swift 3.0 insert in viewDidLoad(), this->
{
view.addSubview(Your_messageInputConteinerView)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification(notification:)), name: .UIKeyboardWillHide, object: nil)
}
func handleKeyboardNotification(notification:Notification){
}