Why doesn't the Clear button align with the text in the UITextField?

I have a UITextField in a UITableViewCell. For some reason, the clear button does not match the text textField. Here is my textField code:


cell.selectionStyle = UITableViewCellSelectionStyleNone;
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(22, 10, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
usernameField.adjustsFontSizeToFitWidth = YES;
usernameField.placeholder = @"Username";
usernameField.backgroundColor = [UIColor clearColor];
usernameField.keyboardType = UIKeyboardTypeDefault;
usernameField.autocapitalizationType = UITextAutocorrectionTypeNo;
usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameField.returnKeyType = UIReturnKeyNext;
usernameField.delegate = self;
usernameField.clearButtonMode = UITextFieldViewModeAlways;
[usernameField setEnabled:YES];
[cell addSubview:usernameField];
[usernameField becomeFirstResponder];
[usernameField release];

And here is a picture to demonstrate the problem: Screen shot

Edit: I also tried with - (CGRect)clearButtonRectForBounds:(CGRect)boundsand nothing yet

+5
source share
1 answer

What is your contentVerticalAlignment property? I saw that this problem appears when the clear button is centered, but the text is aligned at the top. Try adjusting the alignment of the text field to the following:

usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

You're lucky?

+19
source

All Articles