*如何让键盘上方的 EditBar 随着键盘移动?
(1)添加监听、监听事件:
//MARK: - 编辑bar相关
extension PublishDynamicController {
//添加键盘的监听
func addKeyboardObserver() {
//添加键盘监听
NotificationCenter.default.addObserver(self, selector:#selector(self.keyboardWillChangeFrakeyboardWillChangeFrameNotificationmeNotification(note:)),name:NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardWillChangeFrakeyboardWillChangeFrameNotificationmeNotification(note : NSNotification) {
// 1.获取动画执行的时间
let duration = note.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
// 2.获取键盘最终y值
let endFrame = (note.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let y = endFrame.origin.y
// 3.计算工具栏距离底部的间距
let margin = UIScreen.main.bounds.height - y
// 4.执行动画
bottomHeightCons.constant = margin
UIView.animate(withDuration: duration) {
self.view.layoutIfNeeded()
}
}
}
(2)移除监听:
deinit {
NotificationCenter.default.removeObserver(self)
}
网友评论