Why doesn't the clear button aligned with the text in a UITextField?

人走茶凉 提交于 2019-12-07 05:26:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!