以编程方式将UITextField设置为FirstResponder时,会对文本编辑造成一些奇怪的操作 [英] When set UITextField as FirstResponder programmatically, cause some weird actions on text editing
问题描述
我有一个带自定义键盘的UITextField,基本上就像一个计算器。我希望我的键盘默认显示,所以我在 viewDidLoad
中使用 [self.topInputTextField becomeFirstResponder];
认为这是非常常见的用法。但是,它会在我的文本字段上引起一些非常奇怪的操作。
I have a UITextField with custom keyboard, basically like a calculator. I would like my keyboard show up by default, so i used [self.topInputTextField becomeFirstResponder];
in viewDidLoad
, I think that's very common usage. However, it causes some very weird actions on my textfield.
当我将textfield设置为viewDidLoad中的第一个响应者时,每次完成编辑后,文本将会跳转,当我单击另一个文本字段并再次单击第一个文本字段时,第一个文本字段中的文本有时会向下移动并消失,但有时不会。我觉得这很难描述,所以我为它录制了一个GIF图像。
When i set my textfield as the first responder in viewDidLoad, and every time after i done editing, the text will jump, and when i click another text field and and click the first text field again, the texts in the first text field sometimes shift down and disappear, but sometimes not. I feel it's very hard to describe, so i recorded a GIF image for it.
原因是,我确定 [self.topInputTextField becomeFirstResponder];
导致问题,是当我评论该行代码时,一切恢复正常。这是我在评论该行后的GIF:
And the reason, that I am sure [self.topInputTextField becomeFirstResponder];
causing the issue, is when i comment that line of code out, everything back to normal. here is the GIF after i comment out that line:
这对我来说很奇怪,在2 GIF文件之间,我所做的唯一改变是注释掉那行代码。我在SE上找不到任何解决方案。任何想法都将非常感激。
that's vert strange to me, between 2 GIF file, the only change i did is comment out that line of code. I couldn't find any solution on SE. Any idea would be very appreciated.
编辑:
还有一件事是我尝试更改字体和字体大小,它们都有类似的奇怪行为。
One more thing is I tried to change font, and font sizes, they all have similar strange behaviors.
*编辑2:**
这里是我设置文本字段的方式,我没有做任何花哨的事情
*Edit 2:** here is how i set up my textfield,i didn't do anything fancy
推荐答案
尝试在 viewDidAppear
中调用键盘,在 viewDidLoad
之后调用此方法。我认为这是因为你只应该在用户看似已经加载的视图上调用键盘,所以如果你在视图实际出现之前调用它会导致意外行为。
Try calling the keyboard in viewDidAppear
, this method gets called after viewDidLoad
. I assume it's because you should only call the keyboard on a loaded view that has appeared to the user, so if you call it before the view actually appears it will cause unexpected behaviour.
这篇关于以编程方式将UITextField设置为FirstResponder时,会对文本编辑造成一些奇怪的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!