为什么清除按钮不与 UITextField 中的文本对齐?

发布于 2024-09-13 14:43:54 字数 1066 浏览 2 评论 0原文

我在 UITableViewCell 中有一个 UITextField。 由于某种原因,清除按钮未与文本字段的文本对齐。 这是我的文本字段代码:


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];

这是演示问题的图片: Screenshot

编辑: 我也尝试过 - (CGRect)clearButtonRectForBounds:(CGRect)bounds 但仍然没有任何结果

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:
Screenshot

Edit:
I also tried with - (CGRect)clearButtonRectForBounds:(CGRect)bounds and still nothing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2024-09-20 14:43:54

您的 contentVerticalAlignment 属性是什么?当清除按钮居中对齐但文本顶部对齐时,我发现此问题出现。尝试将文本字段对齐方式设置为以下内容:

usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

运气好吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文