NotificationCenter.default.addObserver(self,selector:#selector(self.kbFrameChanged(_:)),name:.UIKeyboardWillChangeFrame, object: nil)
//键盘弹出时,根视图跟随上移.
@objc func kbFrameChanged(_ notification : Notification){
let info = notification.userInfo
let kbRect = (info?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let offsetY = kbRect.origin.y - UIScreen.main.bounds.height
UIView.animate(withDuration: 0.3) {
self.view.transform = CGAffineTransform(translationX: 0, y: offsetY)
//键盘上弹时候, 将返回 button 下移同样的位置,确保在弹出键盘期间可以返回.
self.backBt.transform = CGAffineTransform(translationX: 0, y: -offsetY)
}
}