我使用以下代码显示带有文本字段的 UIAlertView:
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Rename", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* textField = [alertView textFieldAtIndex:0];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
[alertView show];
在 iOS 5.1 上结果如下所示:
在 iOS 6.1 上:
如您所见,清除按钮比在 iOS 6.1 上应有的高度高了一点。知道如何正确居中按钮吗?添加 textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 也无济于事。
(显然,这是 iOS 6.1 中的一个错误,但也许有人知道解决方法。此外,警报 View 中文本字段的高度似乎在不同的操作系统版本中也有所不同,但这是另一个问题。)
最佳答案
这看起来像是 Apple 的错误,但您始终可以在 textField 中添加自己的清除按钮,执行如下操作:
首先创建一个 UITextField 属性:
@property (nonatomic, strong) UITextField *textField;
然后赋值给alertView的textField
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Rename", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
self.textField = [alertView textFieldAtIndex:0];
self.textField.delegate = self;
//Create a custom clear button
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 19, 19)];
[button setImage:[UIImage imageNamed:@"clearButton.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(clearText) forControlEvents:UIControlEventAllTouchEvents];
[self.textField setRightView:button];
self.textField.rightViewMode = UITextFieldViewModeAlways;
然后在clearText方法中,
- (void)clearText {
[self.textField setText:@""];
}
我试过了,效果不错
希望对你有帮助
关于ios - UIAlertView 中的 UITextField : clear button is not centered vertically in iOS 6. 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14758706/