问题描述
我在 UITableViewCell 中有一个 UITextField.出于某种原因,清除按钮未与 textField 的文本对齐.这是我的 textField 代码:
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 = @"用户名";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];[用户名字段成为FirstResponder];[用户名字段发布];这是演示问题的图片:
我也尝试过 - (CGRect)clearButtonRectForBounds:(CGRect)bounds
但仍然没有
你的 contentVerticalAlignment 属性是什么?我已经看到当清除按钮居中对齐但文本顶部对齐时出现此问题.尝试将您的文本字段对齐方式设置为以下内容:
usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
有什么运气吗?
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
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?
这篇关于为什么清除按钮不与 UITextField 中的文本对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!