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))
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) }