为什么在 textViewDidBeginEditing 之前调用观察者 onKeyboardDisplayed

Why observer onKeyboardDisplayed is called before textViewDidBeginEditing

我的应用程序在 swift。当我编辑 UITextField 时,有时键盘会隐藏该字段。因此,我使用委托 textFieldDidBeginEditing 来设置“activeTextField”(并使用 textFieldDidEndEditing 将其重置为 nil)。然后在 viewDidLoad 上,我添加了一个链接到 onKeyboardDisplayed 函数的观察者,我在其中测试“activeTextField”的值,以便在需要时向上滑动屏幕。而且效果很好:)

坏消息是我尝试对 UITextView 执行相同的操作,使用委托 textViewDidBeginEditing 设置“activeTextView”。但与 UITextField 不同的是,委托是在 onKeyboardDisplayed 之后调用的,因此键盘仍然隐藏了我的 UITextView。

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

@objc func onKeyboardDisplayed(notification: Notification) {
    guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
        return
    }
    var visibleRect : CGRect = self.view.frame
    visibleRect.size.height -= keyboardRect.height
    if (activeTextField != nil) {
        // Get y position of active textField bottom.
        let textFieldBottomPosition = activeTextField!.convert(CGPoint.zero, to: nil).y + activeTextField!.frame.height
        if(textFieldBottomPosition > visibleRect.size.height) {
            // swipe up
            view.frame.origin.y = (visibleRect.size.height - textFieldBottomPosition - 6)
        }
    }
    if (activeTextView != nil) {
        // Get y position of active textView bottom.
        let textViewBottomPosition = activeTextView!.convert(CGPoint.zero, to: nil).y + activeTextView!.frame.height
        if(textViewBottomPosition > visibleRect.size.height) {
            // swipe up
            view.frame.origin.y = (visibleRect.size.height - textViewBottomPosition - 6)
        }
    }
}

你知道解决这个问题的方法吗?

键盘出现的标准处理方式是这样

在你的ViewController中:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow), name: UIControl.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillHide), name: UIControl.keyboardWillHideNotification, object: nil)
 }

 @objc private func handleKeyboardWillShow(notification: NSNotification){
    
    guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else{
        return
    }
    self.view.frame.origin.y -= keyboardSize.height
}

@objc private func handleKeyboardWillHide(notification: NSNotification){
    self.view.frame.origin.y = 0
}

这会根据键盘的高度上下移动视图框。 如果我没看错你的问题,相信能帮到你

最后我在这里找到了解决方案:Keyboard events called before UITextView delegate events

我更改了 keyboardWillShowNotification

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

通过 keyboardDidShowNotification

NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardDisplayed(notification:)), name: UIResponder.keyboardDidShowNotification, object: nil)

现在它运行良好:我的 onKeyboardDisplayed 函数在委托 textViewDidBeginEditing

之后被调用