当我点击文本视图两次时,键盘将显示通知不起作用



我在屏幕顶部有一个文本视图,在屏幕底部有一个类似工具栏的自定义视图。当用户单击文本视图时,键盘出现,工具栏随键盘一起向上移动。然后我在工具栏上有一个驳回按钮,它调用了文本视图上的结束功能。当我第二次点击文本视图时,工具栏不会出现,但我需要它出现。这是我附加到自定义工具栏上的代码,使其向上移动:

extension UIView{
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(_ notification: NSNotification){
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endFrame.origin.y - beginningFrame.origin.y

UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {
if UIDevice().userInterfaceIdiom == .phone && UIScreen.main.nativeBounds.height == 2436 {//check user device
//iPhone X
self.frame.origin.y += deltaY + 35
}else{
self.frame.origin.y += deltaY
}
}, completion: nil)
}
@objc func keyboardWillHide(_ notification: NSNotification){
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endFrame.origin.y - beginningFrame.origin.y

UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {
if UIDevice().userInterfaceIdiom == .phone && UIScreen.main.nativeBounds.height == 2436 {//check user device
//iPhone X
self.frame.origin.y = 0
}else{
self.frame.origin.y = 0
}
}, completion: nil)
}
}

当键盘出现时,这是我的文本视图的代码:

override func viewDidLoad() {
super.viewDidLoad()
self.editView.bindToKeyboard()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

@objc func keyboardWillShow(_ notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
textView.contentInset = contentInsets
}
}
@objc func keyboardWillHide(_ notification: NSNotification) {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
textView.contentInset = contentInsets
}

对于键盘显示状态,请使用UIKeyboardWillShowNotification。

NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow) , name: UIResponder.keyboardWillShowNotification, object: nil)

最新更新