本文介绍了消失了UITextField文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是UITextField中的文本在iOS上的每个其他键盘点击都会消失。这个问题发生在多个视图控制器中,但之前我已经能够通过使文本字段更高来解决问题,为其提供更多空间来绘制文本。

The issue I am having is that text in a UITextField disappears every other keyboard tap on iOS. This issue has happened in multiple view controllers, but previously I have been able to resolve the issue by making the textfield taller, giving it more room to draw the text inside.

该解决方案不适用于这种特殊情况。我正在为我的应用程序使用自定义字体,所以我使用了UITextField的子类来自动设置自定义字体的文本字段。我已经尝试恢复到基本的UITextField,但这并没有解决问题。

That solution is not working for this particular situation. I am using a custom font for my app, so I have used a subclass of UITextField for the text fields that automatically sets the custom font. I have tried reverting back to the basic UITextField, but this does not solve the problem.

我也试过注释掉我在问题视图中实现的委托方法控制器,这并没有解决我的问题。我真的不知道还有什么可以尝试,所以我希望这里的其他人可以指出我正确的方向。以下是我到目前为止发现的关于我的问题的事情:

I have also tried commenting out the delegate methods I've implemented in the problem view controller and this does not solve my issue. I don't really know what else to try, so I'm hoping someone else here can point me in the right direction. Here are the things I've discovered about my problem so far:


  1. 文本消失并在每次按键时重新出现,但点击退格按钮不会更改文本的外观/消失(即,如果在文本可见时点击退格键,则正常删除文本。如果在隐藏文本时点击,则会从文本字段中删除字符,但用户无法看到发生了什么)

  2. 文本显示在文本字段底部,文本字段是第一个响应者

  3. 当另一个文本字段变为第一响应者,有问题的文本字段中的文本将被正确绘制,在文本字段中垂直居中,并始终可见。

  4. 在文本中包含文本时创建文本字段第一响应者已经导致要重绘的文本,并显示为在文本字段中向上/向下滚动。文本将交替地以文本字段为中心绘制,并从视图中向下滚动,或者从视图中绘制出来并滚动到视图中,放在文本字段的底部。

  5. 视图控制器只在其呈现的两种模式之一中出现问题。它或者以模态方式呈现为UINavigationController的根视图控制器,或者作为导航控制器的第三视图被推送到模态呈现的UINavigationController的堆栈中控制器。在第一种情况下,它是根视图控制器,文本字段工作得很好并且没有表现出上述的奇怪行为。在第二种情况下,奇怪的行为始终如一。

  1. The text disappears and reappears with every key tap, though tapping the backspace button does not change the appearance/disappearance of the text (i.e. if tapping backspace when the text is visible, text is deleted normally. If tapping while the text is hidden, characters are deleted from the textfield's text, but the user can't see what's happening)
  2. Text appears at the bottom of the textfield when it is visible and the textfield is the first responder
  3. When a different text field becomes first responder, the text in the problematic text field will be drawn properly, centered vertically in the textfield, and always visible.
  4. Making a textfield first responder when it has text in it already will cause the text to redraw and appear as if it is scrolling up/down within the textfield. The text will alternately be drawn centered in the textfield and "scroll" down out of view, or be drawn out of view and "scroll" up into view, resting at the bottom of the textfield.
  5. The view controller only has problems in one of the two modes it is presented in. It is either presented modally as the root view controller of a UINavigationController, or it is pushed onto a modally-presented UINavigationController's stack as the navigation controller's third view controller. In the first case, where it is the root view controller, the textfields work perfectly fine and exhibit none of the strange behavior described above. The strange behavior occurs consistently in the second case.

在这个类似问题中发布的视频中可以看到这个问题的一个很好的例子:。问题的答案本身并没有用,也无法解决我的问题。

A good example of this problem can be seen in the video posted in this similar question: UITextField text disappears on every other keystroke. The answers to the question itself are not useful and do not solve my problems.

我是成功地重现了我之前遇到的问题。事实证明,问题是由于在导航控制器的视图中添加了一个子视图,然后在UITextField聚焦时将其删除。如果在未编辑UITextField时删除子视图,则不会发生错误。

I've been successful in reproducing the issue that I was having before. It turns out that the issue was caused by adding a subview to the navigation controller's view, then removing it while a UITextField was in focus. If the subview is removed while the UITextField is not being edited, the bug will not occur.

在我的情况下,加载图像作为子视图添加到导航控制器在网络通话期间,并在完成所述通话后删除。在此期间,UITextField将处于活动状态,这将导致我在上面描述的视觉错误。

In my case, a loading image was added as a subview to the navigation controller during a net call, and removed after the completion of said call. During this time, a UITextField would be active, which would cause the visual bug I describe above.

我在这里上传的独立示例项目中重新创建了错误:。另请注意,如果您告诉textField成为视图控制器的viewDidLoad实现中的第一响应者,则此错误将在开始时发生并持续存在。

I've recreated the bug in a standalone example project I've uploaded here: https://www.dropbox.com/s/s4kc05vg1pwteeo/Funky%20TextField%20Tester.zip. Also note that if you tell the textField to become first responder in the viewDidLoad implementation for the view controller, this bug will occur right at the beginning and persist.

我很容易避免将加载视图放在导航控制器的视图中,但这样做会给我一个很好的效果,即加载视图'float'屏幕上的相同位置,而将其放入导航堆栈中的视图控制器视图会引发一系列视觉问题(当视图控制器被压入堆栈时移动加载视图位置,加载视图被其他视图控制器遮挡等)。

It's easy enough for me to just avoid putting the loading view in the navigation controller's view, but doing so gives me a nice effect of having the loading view 'float' in the same position on the screen, whereas putting it into a view controller's view on a nav stack raises a bunch of visual issues (moving loading view position when view controller is pushed onto stack, loading view is obscured by other view controllers, etc.).

有没有办法在不将视图放入导航控制器视图的情况下重现这种外观,或者可能是一种安全的方式将其放入导航控制器的视图而不执行奇怪的解决方法,比如在子视图的演示期间停止所有UITextFields的编辑?

Is there any way to reproduce this kind of look without putting the view into the navigation controller's view, or perhaps a safe way to put it into the navigation controller's view without performing strange workarounds like stopping editing on all UITextFields in the duration of the subview's presentation?

推荐答案

我在这里遇到同样的问题,在之前的版本中没有。
感谢你的项目我测试过,我发现这个问题来自于一个带有半透明导航栏的导航控制器中嵌入的视图控制器。

I've got the same issue here, didn't have in a previous version. thanks to your project I've test, I've found that the issue is coming from presenting a view controller embedded in a navigation controller with a translucent navigation bar.

我在我呈现的导航控制器的模拟指标中设置了不透明条,并且UITextField没有更多问题。

I set opaque bar in the simulated metrics of the navigation controller I was presenting and no more issue with UITextField.

至少在我的情况下,一切都在我在InterfaceBuilder中为我的UINavigationController设置模拟指标顶部栏的不透明栏的时刻,它嵌入了一个UIViewController,我想以模态方式呈现

On my case at least, everything were resolved at the moment I set opaque bar for top bar in simulated metrics in InterfaceBuilder for my UINavigationController which embed a UIViewController I've wanted to present modally

希望它是相同的你也是。

Hope it will be the same for you as well.

这篇关于消失了UITextField文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:28