首先設置系統廣播監聽鍵盤變化
NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(notify:)), name:UIResponder.keyboardWillChangeFrameNotification , object: nil)
}
鍵盤事情響應修改輸入框的底部約束
// 鍵盤監聽事件
@objc func keyboardChange(notify:NSNotification)
{
// 1.取出鍵盤最終的rect
let value = notify.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue
// 鍵盤彈出時的Y值
let rectY = value.cgRectValue.origin.y
// 2.修改工具條的約束
let height = UIScreen.main.bounds.height
let margin = height - rectY
TabBarViewBottom.constant = margin
// 3.更新界面
let duration = notify.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
UIView.animate(withDuration: TimeInterval(duration)) {
self.view.layoutIfNeeded()
}
}