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

时间:2020-12-23 11:46:31

标签: swift uitextview uikeyboard uitextviewdelegate

我的应用程序运行迅速。当我编辑 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)
        }
    }
}

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

2 个答案:

答案 0 :(得分:2)

最后我在这里找到了解决方案: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)

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

答案 1 :(得分:1)

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

在您的视图控制器中:

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
}

这会根据键盘的高度上下移动视图框架。 如果我正确理解您的问题,我相信这可以帮助您

相关问题