Andrew Schreiber
Andrew Schreiber

Reputation: 14920

UITextField text disappears on every other keystroke

Here is a video of the phenomena: http://youtu.be/c0TP8YVF9TE

As the video shows, the value in exampleTextView.text is not lost. Its just hidden every other keystroke.

Solutions tried: I do not set the exampleTextView.text value to something during the program, except after the return key is pressed (and commenting that line out changes nothing).

I have set:

self.exampleTextView.clearsOnBeginEditing=NO;
self.exampleTextView.clearsContextBeforeDrawing=NO;
self.exampleTextView.clearsOnInsertion=NO;

Edit: Here is my code: https://gist.github.com/andrewschreiber/6970283

Upvotes: 13

Views: 5019

Answers (6)

ssynhtn
ssynhtn

Reputation: 1377

@SbClx 's solution works for me, but I have another workaround that also works: set the background of the UITextField to any UIImage you like.

After some trial and error, I found that the bug disappears when I set the borderStyle of UITextField to UITextBorderStyleRoundedRect, which is unfortunately not what I wanted, I want a simple white background.

The documentation of UITextField.borderStyle states that: If the value is set to the UITextBorderStyleRoundedRect style, the custom background image associated with the text field is ignored. So I guess background image might have something to do with the bug too.

Upvotes: 0

user3339688
user3339688

Reputation: 31

Moving the UITextField's becomeFirstResponder from viewWillAppear to viewWillLayoutSubviews fixed this for my case. I also made sure to add resignFirstResponder on the UITextField to the actions that were dismissing the view (which was presented modally).

Upvotes: 2

René Fischer
René Fischer

Reputation: 468

I was facing the same problem. My solution was to remove [_textField becomeFirstResponder] from the - (void)viewWillAppear:(BOOL)animated method. This bug only occurs if the viewcontroller was presented modally.

Upvotes: 11

SbClx
SbClx

Reputation: 326

I had almost the same problem. Sometimes the text was disappearing.

I solved it after changing the position of the UITextField in the view hierarchy in the xib file.

enter image description here

Upvotes: 31

Andrew Schreiber
Andrew Schreiber

Reputation: 14920

Solved the problem.

On my storyboard, I switched the text from 'Plain' to 'Attributed' then back to 'Plain' again.

Upvotes: 0

nivritgupta
nivritgupta

Reputation: 1966

Try to use all the Delegate method of UITextField . and please let me know what are you trying to do with Done Button clicked , only set the text of anything else or if you share all the code with me then i can help you

Please replace your textFieldShouldReturn method with my method and also implementing the textFieldShouldBeginEditing method in your code and its working fine

 -(BOOL) textFieldShouldReturn:(UITextField *) textField {

          [textField resignFirstResponder];
    }


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {


    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];


    [UIView commitAnimations];

    return YES;


}

Upvotes: 0

Related Questions