1.注册监听事件
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillChangeFrame(node:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
2.执行事件
@objc func keyboardWillChangeFrame(node : Notification){
print(node.userInfo ?? "")
// 1.获取动画执行的时间
let duration = node.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
// 2.获取键盘最终 Y值
let endFrame = (node.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let y = endFrame.origin.y
//3计算工具栏距离底部的间距
let margin = UIScreen.main.bounds.height - y
print("键盘高度",margin)
//4.执行动画
webView_bottom.constant = margin
UIView.animate(withDuration: duration) {
self.view.layoutIfNeeded()
}
}
3.移除监听
deinit {
NotificationCenter.default.removeObserver(self)
}