添加一个scrollview作为基本视图,然后在这个视图中添加contentview
func keyboardShown(_ notification: Notification){
var userInfo = notification.userInfo!
let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize!.height + 40), 0.0)
self.mainScrollView.contentInset = contentInsets
self.mainScrollView.scrollIndicatorInsets = contentInsets
// **-- Scroll when keyboard shows up
let aRect = self.view.frame
self.mainScrollView.contentSize = aRect.size
/* if((self.activeTextField) != nil)
{
self.scrollView.scrollRectToVisible(self.activeTextField!.frame, animated: true)
}*/
}
func keyboardHidden(_ notification: Notification) {
let contentInsets = UIEdgeInsets.zero
self.mainScrollView.contentInset = contentInsets
self.mainScrollView.scrollIndicatorInsets = contentInsets
// **-- Scroll when keyboard shows up
self.mainScrollView.contentSize = self.containerView.frame.size
}