为什么每次选择另一个 TextField 时都会调用 UIKeyboardWillShowNotification? [英] Why is UIKeyboardWillShowNotification called every time another TextField is selected?
问题描述
我有一个项目,其中包含一个 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
仅在我点击键盘上的完成"按钮时调用
The other thing is that UIKeyboardWillHideNotification
is only called when I hit the "Done" button on the keyboard
我肯定不会在任何地方调用任何 resignFirstResponder
、becomeFirstResponder
、endEditing
.(我的意思是不要叫错)
I'm sure to not call any resignFirstResponder
, becomeFirstResponder
, endEditing
anywhere. (I mean not call wrongly)
什么会导致这个问题?
这是堆栈跟踪
推荐答案
问题是我为 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
这篇文章在 iOS 上使用键盘很好地解释了这一点
This article Working With Keyboard on iOS explains this well
当我们将外部键盘连接到iPad.在这种特殊情况下,通知行为取决于在控件的 inputAccessoryView 属性上,这是原因用于显示键盘.
Additional changes take place when we connect an external keyboard to the iPad. In this particular case, the notification behavior depends on the inputAccessoryView property of the control which was the reason for displaying the keyboard.
如果 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.
Whenever new UITextField
is selected, the OS needs to compute the frame for the keyboard again, and the following notifications are posted
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
使用 same 视图将导致 UIKeyboardWillShowNotification
只调用一次
Note that using the same View for inputAccessoryView
will cause UIKeyboardWillShowNotification
only called once
这篇关于为什么每次选择另一个 TextField 时都会调用 UIKeyboardWillShowNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!