Move form on keyboard show/hide event SWIFT
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:",
name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:",
name: UIKeyboardWillHideNotification, object: nil)
func keyboardWasShown (notification: NSNotification) {
let info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
let insets = UIEdgeInsetsMake(0, 0, keyboardSize!.height, 0)
mainView.contentInset = insets
mainView.scrollIndicatorInsets = insets
var aRect = view.frame
aRect.size.height -= keyboardSize!.height
if let activeField = activeField {
if !CGRectContainsPoint(aRect, activeField.superview!.frame.origin) {
mainView.scrollRectToVisible(activeField.frame, animated: true)
}
}
}
func keyboardWillBeHidden (notification: NSNotification) {
let insets = UIEdgeInsetsZero
mainView.contentInset = insets
mainView.scrollIndicatorInsets = insets
}