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.

Complete code to reproduce:
throwaway_testViewController.h:
#import <UIKit/UIKit.h>
@interface throwaway_testViewController : UIViewController <UITextFieldDelegate>{
UITextField * unitField;
}
@property (nonatomic, assign) IBOutlet UITextField * unitField;
-(IBAction)setEditingSectionUnits;
-(void)setEditingSectionValue;
-(IBAction)equalsPress;
@end
throwaway_testViewController.m
#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
In the xib, place a UITextField tied to unitField, and set the delegate of the text field to be file’s owner.
Place a UIButton labeled equalsPress and tie it to that IBAction, and another called edit, tied to setEditingSectionUnits. To see the bug reproduced:
- Run the app
- Press edit
- type something into the text field (min 8-10 characters)
- press enter on the keyboard
- press equalsPress
- press edit
- Should see: cursor and empty text field
- Actually see: whatever you typed last, with a cursor at the start.
- Typing makes this text disappear.
after replicating your code, i found that the problem you mentioned occurs only if you press return on the keyboard and then tap the equals press button…
resigning first responder before calling animation block in your
setEditingSectionValuemethod resolved the problem: