UITextField .
:
extension UIScrollView {
func makeAwareOfKeyboard() {
NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardDidShow), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardDidHide), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}
func keyBoardDidShow(notification: Notification) {
let info = notification.userInfo as NSDictionary?
let rectValue = info![UIKeyboardFrameBeginUserInfoKey] as! NSValue
let kbSize = rectValue.cgRectValue.size
let contentInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0)
self.contentInset = contentInsets
self.scrollIndicatorInsets = contentInsets
}
func keyBoardDidHide(notification: Notification) {
let contentInsets = UIEdgeInsetsMake(0, 0, 0, 0)
self.contentInset = contentInsets
self.scrollIndicatiorInsets = contentInsets
}
}
override func viewDidLoad() {
super.viewDidLoad()
...
scrollView.makeAwareOfKeyboard()
...
}