Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disappearing UITextField text

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.

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. 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.

EDIT

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.

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.

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.

NEW QUESTION

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.).

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?

like image 585
Ziewvater Avatar asked Mar 19 '14 01:03

Ziewvater


1 Answers

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.

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

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.

like image 150
Razmog Avatar answered Sep 21 '22 05:09

Razmog