我需要为 UITextField 创建一个自定义清除按钮(因为我的配色方案)。我正在使用以下代码(其中类是 UITextField 后代):
UIButton *clearButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[clearButton setImage:[UIImage imageNamed:@"ClearButton.png"] forState:UIControlStateNormal];
[self setRightView:clearButton];
[self setRightViewMode:UITextFieldViewModeNever];
[clearButton addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
大多数情况下,这可以正常工作。问题是单击清除按钮会导致 UITextField 退出第一响应者状态(即键盘消失)。我希望 UITextField 保持焦点。
我尝试在清除按钮的操作中恢复第一响应者状态,但是,如果 UITextField 在 UITableView 中 - 并且被滚动 - 这会在键盘关闭和打开时导致表格中出现一些不需要的滚动。
我可以使用 UIImageView 而不是 UIButton,但是当点击按钮时我没有看到视觉“脉冲”。
那么,有没有办法让 UITextField 在按下按钮时保持第一响应者状态?
我正在使用 iOS 6 开发 iPhone 应用程序。
[注意:“清除:”操作目标只是清空文本字段,但是,无论如何,即使我没有设置目标,我的问题也会出现。]