How to make UITextField move up when the keyboard is present?

How to prevent hiding UITextField from keyboard?

+7
ios swift
source share
7 answers

I assume this happens on a UIViewController. If so, you can configure the following 2 functions that will be called when the keyboard is displayed / hidden and react accordingly in its blocks.

Configure UIViewController:

 class XXXViewController: UIViewController, UITextFieldDelegate... { var frameView: UIView! 

Firstly, in ViewDidLoad:

 override func viewDidLoad() { self.frameView = UIView(frame: CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)) // Keyboard stuff. let center: NSNotificationCenter = NSNotificationCenter.defaultCenter() center.addObserver(self, selector: #selector(ATReportContentViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) center.addObserver(self, selector: #selector(ATReportContentViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil) } 

Then perform the following 2 functions to respond to your NSNotificationCenter functions, as described above in ViewDidLoad. I will give you an example of moving the whole view, but you can also animate only UITextFields.

 func keyboardWillShow(notification: NSNotification) { let info:NSDictionary = notification.userInfo! let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue() let keyboardHeight: CGFloat = keyboardSize.height let _: CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber as CGFloat UIView.animateWithDuration(0.25, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { self.frameView.frame = CGRectMake(0, (self.frameView.frame.origin.y - keyboardHeight), self.view.bounds.width, self.view.bounds.height) }, completion: nil) } func keyboardWillHide(notification: NSNotification) { let info: NSDictionary = notification.userInfo! let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue() let keyboardHeight: CGFloat = keyboardSize.height let _: CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber as CGFloat UIView.animateWithDuration(0.25, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { self.frameView.frame = CGRectMake(0, (self.frameView.frame.origin.y + keyboardHeight), self.view.bounds.width, self.view.bounds.height) }, completion: nil) } 

Remember to delete notifications when exiting a view.

 override func viewWillDisappear(animated: Bool) { NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) } 
+12
source share

Here is a simple solution in Swift. I translated the Objective-C code that worked for me in the past.

 func textFieldDidBeginEditing(textField: UITextField) { // became first responder //move textfields up let myScreenRect: CGRect = UIScreen.mainScreen().bounds let keyboardHeight : CGFloat = 216 UIView.beginAnimations( "animateView", context: nil) var movementDuration:NSTimeInterval = 0.35 var needToMove: CGFloat = 0 var frame : CGRect = self.view.frame if (textField.frame.origin.y + textField.frame.size.height + /*self.navigationController.navigationBar.frame.size.height + */UIApplication.sharedApplication().statusBarFrame.size.height > (myScreenRect.size.height - keyboardHeight)) { needToMove = (textField.frame.origin.y + textField.frame.size.height + /*self.navigationController.navigationBar.frame.size.height +*/ UIApplication.sharedApplication().statusBarFrame.size.height) - (myScreenRect.size.height - keyboardHeight); } frame.origin.y = -needToMove self.view.frame = frame UIView.commitAnimations() } func textFieldDidEndEditing(textField: UITextField) { //move textfields back down UIView.beginAnimations( "animateView", context: nil) var movementDuration:NSTimeInterval = 0.35 var frame : CGRect = self.view.frame frame.origin.y = 0 self.view.frame = frame UIView.commitAnimations() } 
+8
source share

In Swift 3 use this code

  override func viewDidLoad() { super.viewDidLoad() let center: NotificationCenter = NotificationCenter.default center.addObserver(self, selector: #selector(RFLogInViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) center.addObserver(self, selector: #selector(RFLogInViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } func keyboardWillShow(notification: NSNotification) { let info:NSDictionary = notification.userInfo! as NSDictionary let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue let keyboardHeight: CGFloat = keyboardSize.height let _: CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber as! CGFloat UIView.animate(withDuration: 0.25, delay: 0.25, options: .curveEaseInOut, animations: { self.view.frame = CGRect(x: 0, y: (self.view.frame.origin.y - keyboardHeight), width: self.view.bounds.width, height: self.view.bounds.height) }, completion: nil) } func keyboardWillHide(notification: NSNotification) { let info: NSDictionary = notification.userInfo! as NSDictionary let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue let keyboardHeight: CGFloat = keyboardSize.height let _: CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber as! CGFloat UIView.animate(withDuration: 0.25, delay: 0.25, options: .curveEaseInOut, animations: { self.view.frame = CGRect(x: 0, y: (self.view.frame.origin.y + keyboardHeight), width: self.view.bounds.width, height: self.view.bounds.height) }, completion: nil) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(true) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) } 
+1
source share

first you must have scrollview Step 1: find the height of the keyboard

 func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo ? [UIKeyboardFrameBeginUserInfoKey] as ? NSValue) ? .cgRectValue { let keyboardHeight = keyboardSize.height a = keyboardHeight } } 

step 2: bind text field delegation methods

 func textFieldDidBeginEditing(_ textField: UITextField) { utility.setUserDefaultBool(value: false, key: "FrameMoveFlag") //flag let b = view1.frame.height - textField.frame.origin.y if (b < 350) { view1.frame.origin.y = view1.frame.origin.y + (view1.frame.height - textField.frame.origin.y) - (a + 50) utility.setUserDefaultBool(value: true, key: "FrameMoveFlag") //flag } } func textFieldDidEndEditing(_ textField: UITextField) { if (utility.getUserDefaultBOOLForKey(key: "FrameMoveFlag") == true) { view1.frame.origin.y = 0 } } 
0
source share

Swift 3.0

  var activeField: UITextField? override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(ProfileViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(ProfileViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } func textFieldDidBeginEditing(_ textField: UITextField){ activeField = textField } func textFieldDidEndEditing(_ textField: UITextField){ activeField = nil } func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { if (self.activeField?.frame.origin.y)! >= keyboardSize.height { self.view.frame.origin.y = keyboardSize.height - (self.activeField?.frame.origin.y)! } else { self.view.frame.origin.y = 0 } } } func keyboardWillHide(notification: NSNotification) { self.view.frame.origin.y = 0 } 
0
source share

If you use UIScrollView or any of its subclasses, such as UITableView , you can also control the contentInset property. This way you don't have to bother with frame , bounds , NSLayoutConstraint or NSLayoutAnchor .

 func keyboardWillShow(notification: Notification) { let info = notification.userInfo! let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue let keyboardHeight: CGFloat = keyboardSize.height let duration = info[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval UIView.animate(withDuration: duration, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: { self.tableView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0) }, completion: nil) } func keyboardWillHide(notification: Notification) { let info = notification.userInfo! let duration = info[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval UIView.animate(withDuration: duration, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: { self.tableView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) }, completion: nil) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) } 
0
source share

Swift 3 for Geiger Response

 func textFieldDidBeginEditing(_ textField: UITextField) { // became first responder //move textfields up let myScreenRect: CGRect = UIScreen.main.bounds let keyboardHeight : CGFloat = 216 UIView.beginAnimations( "animateView", context: nil) var movementDuration:TimeInterval = 0.35 var needToMove: CGFloat = 0 var frame : CGRect = self.view.frame if (textField.frame.origin.y + textField.frame.size.height + UIApplication.shared.statusBarFrame.size.height > (myScreenRect.size.height - keyboardHeight - 30)) { needToMove = (textField.frame.origin.y + textField.frame.size.height + UIApplication.shared.statusBarFrame.size.height) - (myScreenRect.size.height - keyboardHeight - 30); } frame.origin.y = -needToMove self.view.frame = frame UIView.commitAnimations() } func textFieldDidEndEditing(_ textField: UITextField) { //move textfields back down UIView.beginAnimations( "animateView", context: nil) var movementDuration:TimeInterval = 0.35 var frame : CGRect = self.view.frame frame.origin.y = 0 self.view.frame = frame UIView.commitAnimations() } 
0
source share

All Articles