Move the view Up when keyboard shows in iOS swift.
Solution:
If you are tried to edit the textField but keyboard came up and hides the TextField.
Don't worry here below the code will broken your headache.It will up the view when the keyboard came to visible.
In ViewdidLoad add the below code
Then implement the below methods to get the edit easy.
If you are tried to edit the textField but keyboard came up and hides the TextField.
Don't worry here below the code will broken your headache.It will up the view when the keyboard came to visible.
In ViewdidLoad add the below code
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)Then implement the below methods to get the edit easy.
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
}
Comments
Post a Comment