Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get rid of "ghost" text that appears in UITextField after clearing the field programmatically?
    primarykey
    data
    text
    <p>I have a strange problem with a UITextField - I am setting the value of it to @"" (or anything else still causes it) and then shrinking the field with to zero. Next time I unshrink it, it displays the same value it had before I shrunk it, regardless of my having changed the text in the view. Typing in the field causes the glitch to go away, but it looks bad.</p> <p><img src="https://i.stack.imgur.com/2fxQ5.png" alt="Image showing the problem"></p> <p>Complete code to reproduce:</p> <p>throwaway_testViewController.h:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface throwaway_testViewController : UIViewController &lt;UITextFieldDelegate&gt;{ UITextField * unitField; } @property (nonatomic, assign) IBOutlet UITextField * unitField; -(IBAction)setEditingSectionUnits; -(void)setEditingSectionValue; -(IBAction)equalsPress; @end </code></pre> <p>throwaway_testViewController.m</p> <pre><code>#import "throwaway_testViewController.h" @implementation throwaway_testViewController @synthesize unitField; #pragma mark - Inputs -(IBAction)equalsPress{ [UIView animateWithDuration:0.5 animations:^(void){ [unitField setText:@""]; [self setEditingSectionValue]; }]; } #pragma mark Input Control -(void)setEditingSectionUnits{ [UIView animateWithDuration:0.5 animations:^(void){ CGRect newRect = unitField.frame; newRect.size.width = 160; unitField.frame = newRect; }completion:^(BOOL completed){ completed ? [unitField setNeedsDisplay] : nil; }]; [unitField becomeFirstResponder]; } -(void)setEditingSectionValue{ [UIView animateWithDuration:0.5 animations:^(void){ CGRect newRect = unitField.frame; newRect.size.width = [unitField.text sizeWithFont:unitField.font constrainedToSize:CGSizeMake(80, 250)].width;; unitField.frame = newRect; }completion:^(BOOL completed){ completed ? [unitField setNeedsDisplay] : nil; }]; [unitField resignFirstResponder]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [self setEditingSectionValue]; return TRUE; } -(void)textFieldDidBeginEditing:(UITextField *)textField{ [self setEditingSectionUnits]; } @end </code></pre> <p>In the <code>xib</code>, place a <code>UITextField</code> tied to <code>unitField</code>, and set the delegate of the text field to be file's owner.</p> <p>Place a <code>UIButton</code> labeled <code>equalsPress</code> and tie it to that <code>IBAction</code>, and another called edit, tied to <code>setEditingSectionUnits</code>. To see the bug reproduced:</p> <ul> <li>Run the app</li> <li>Press edit</li> <li>type something into the text field (min 8-10 characters)</li> <li>press enter on the keyboard</li> <li>press equalsPress</li> <li>press edit</li> <li>Should see: cursor and empty text field</li> <li>Actually see: whatever you typed last, with a cursor at the start.</li> <li>Typing makes this text disappear.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload