问题
I have an UITextField in an UITableViewCell. For some reason the clear button isn't align to the textField's text. Here's 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's the picture to demonstrate the problem:
Edit:
I also tried with - (CGRect)clearButtonRectForBounds:(CGRect)bounds and still nothing
回答1:
What is your contentVerticalAlignment property? I've seen this issue show up when the clear button is center aligned but the text is aligned the top. Try setting your textfield alignment to the following:
usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Any luck with that?
来源:https://stackoverflow.com/questions/3437884/why-doesnt-the-clear-button-aligned-with-the-text-in-a-uitextfield