// Example of using KeyboardNotification to update an NSLayoutConstraint import UIKit final class ViewController: UIViewController { // Outlet for a layout constraint that specifies distance from bottom of // a subview to the bottom of the view. // // This constraint will be updated when the keyboard appears, disappears, // or changes size. @IBOutlet weak var bottomLayoutConstraint: NSLayoutConstraint! override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillChangeFrameNotification:", name: UIKeyboardWillChangeFrameNotification, object: nil) } override func viewDidDisappear(animated: Bool) { NSNotificationCenter.defaultCenter().removeObserver(self) super.viewDidDisappear(animated) } func keyboardWillChangeFrameNotification(notification: NSNotification) { let n = KeyboardNotification(notification) let keyboardFrame = n.frameEndForView(self.view) let animationDuration = n.animationDuration let animationCurve = n.animationCurve let viewFrame = self.view.frame let newBottomOffset = viewFrame.maxY - keyboardFrame.minY self.view.layoutIfNeeded() UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions(rawValue: UInt(animationCurve << 16)), animations: { self.bottomLayoutConstraint.constant = newBottomOffset self.view.layoutIfNeeded() }, completion: nil ) } }