问题描述
我的项目中包含 UIScrollView
和许多 UITextField
。
I have a project that contains a UIScrollView
and many UITextField
inside it.
我第一次选择 UITextField
,调用 UIKeyboardWillShowNotification
,这很好。但每当我选择新的 UITextField
(键盘仍在那里)时,再次调用 UIKeyboardWillShowNotification
,这是很奇怪。
For the first time I select a UITextField
, UIKeyboardWillShowNotification
is called, which is fine. But whenever I select new UITextField
(THE KEYBOARD IS STILL THERE), UIKeyboardWillShowNotification
is called again !!!, which is weird.
我还为 [UIResponder resignFirstResponder]
设置了一个符号断点,我看到它在之前被击中了在之后调用UIKeyboardWillShowNotification
!!!
I also set a symbolic breakpoint for [UIResponder resignFirstResponder]
and I see that it is hit before and after UIKeyboardWillShowNotification
is called !!!
另一件事是 UIKeyboardWillHideNotification $只有当我点击键盘上的完成按钮时才会调用c $ c>
我肯定不会调用任何 resignFirstResponder
, becomeFirstResponder
, endEditing
任何地方。 (我的意思是不要错误地打电话)
I'm sure to not call any resignFirstResponder
, becomeFirstResponder
, endEditing
anywhere. (I mean not call wrongly)
什么会导致这个问题?
这是stacktrace
Here is the stacktrace
推荐答案
问题是我为 UITextField
设置了 inputAccessoryView
,这个原因是 UIKeyboardWillShowNotification
选择新的 UITextField
时再次调用
The problem is I set inputAccessoryView
for the UITextField
, and this cause UIKeyboardWillShowNotification
being called again when new UITextField
is selected
本文很好地解释了这一点
This article Working With Keyboard on iOS explains this well
如果是inputAccessoryView不存在或其高度等于0
点,不发送键盘通知。我猜这是
,因为在这种情况下,应用程序中不会发生视觉变化。
否则,所有通知都按预期运行 - 这意味着当键盘显示
或隐藏在正常(未取消停靠或拆分)状态时,大多数情况下它们都是
。
If inputAccessoryView is not present or its height is equal to 0 points, no keyboard notifications are sent. My guess is that this is because in this case, no visual changes take place in application. Otherwise, all notifications behave as expected – which means they are being sent as in the majority of cases when the keyboard is displayed or hidden in a normal (not undocked or split) state.
每当选择新的 UITextField
时,操作系统需要计算再次键盘,并发布以下通知
Whenever new UITextField
is selected, the OS needs to compute the frame for the keyboard again, and the following notifications are posted
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
同样适用于TextField失去其第一响应者状态
The same applies for when the TextField loses its first responder status
请注意,使用相同查看 inputAccessoryView
将导致 UIKeyboardWillShowNotification
仅称为一次
Note that using the same View for inputAccessoryView
will cause UIKeyboardWillShowNotification
only called once
这篇关于为什么每次选择另一个TextField时都会调用UIKeyboardWillShowNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!