I have a strange problem. I have an UITextField in which the user should write the amount of something, so the field is called "amountField". Everything looks fine, when the user starts editing the textfield the text is in the vertical and horizontal center - that's great.
However, when the user ends editing the text moves up a little bit. I tried many things, nothing helped...
I am adding screenshots below, so you can see what is the problem.
This is what it looks like while editing the field - that's ok.
And this is how it looks when done editing - that is the problem!
Please, if anybody know what could cause this I would be very grateful! :)
Here is some of my code related to the amountField.
amountField.keyboardType = UIKeyboardTypeNumberPad;
amountField.returnKeyType = UIReturnKeyDone;
amountField.delegate = self;
[amountField setFont:[UIFont fontWithName:@"Nuptial Script LT Std" size:30]];
amountField.borderStyle = UITextBorderStyleNone;
UIImage *amountBg = [UIImage imageNamed:@"skin2_ipad_amountField.png"];
[amountField setBackground:amountBg];
amountField.rightView = nil;
//amountField.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.2];
amountField.textAlignment = UITextAlignmentCenter;
amountField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
amountField.adjustsFontSizeToFitWidth = YES;
amountLabel.textColor = UIColorFromARGB(0x313030); //Using my own macro
amountField.frame = CGRectMake(300, 480, 136, 32);
amountField.center = CGPointMake(605, 439);
PS: Those white corners are there because I set the background to white with 0.2 alpha, that's ok.
I had a similar issue that started happening on iOS 9. Basically I have a UITextField in a collection view cell. Sometimes when the user is done typing and editing ends, the text "bounces" up then down again into its correct position. Very strange and annoying glitch. Simply making this tweak fixed the issue on iOS 9 and proved to be safe on iOS 7 and 8:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField layoutIfNeeded]; //Fixes iOS 9 text bounce glitch
//...other stuff
}
So...
After many hours of trying many things - I have found the problem. In my case the problem is the font. I really don't know why, but the author of the font made the font weird (leading etc.), it has a blank space on the bottom. I don't know why, but when you are editing the text all of the text properties are ignored, but after you finish editing, they are applied.
So, if you have a similar problem, try changing the font to Arial or something similar.
For a full explanation, please consult these following links: link 1 and link 2. The solution recommended in these links can avoid you a lot of headaches and can even be applied to fix problem like text moving to the top when you start editing an UITextField (using System font or other particular fonts).
Disabling ClipsToBounds for the TextField solved it for me.
This bug happened to me when I set text & became the first responder in viewDidLoad
or viewWillAppear
. When I moved the becomeFirstResponder
code to viewDidAppear
the bug went away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With