Adjust for the Keyboard in iOS Using Swift
Let's say you have an app with a text view which should just take all the space possible without being beneath the keyboard.
Fist add constraints which pin the text view to all four edges of the super view.
Ctr-drag from the bottom constraint to the view controller to make an outlet for the constraint. This will allow you to change the constant of the bottom constraint.
Add the view controller as an observer for the notification UIKeyboardWillShowNotification in viewDidLoad() and set the constant of the bottom constraint to 0.0 to make the text view take all the available space at the beginning:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
textViewBottomConstraint.constant = 0.0
}
Add the method which gets called when the notification is triggered by the system:
func keyboardWillShow(sender: NSNotification) {
if let userInfo = sender.userInfo {
if let keyboardHeight = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size.height {
textViewBottomConstraint.constant = keyboardHeight
UIView.animateWithDuration(0.25, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}
}
}
The code does
- get the userInfo dictionary from the notification (line 2)
- get the height of the keyboard (line 3)
- set the constant of the bottom constraint (line 4)
- animate to the value of the constraint (line 5-7)
Done!

