iOS UITextField右下角按钮触发事件
1、创建一个UITextField对象
-(UITextField *)searchTF{
if (!_searchTF) {
_searchTF = ({
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectZero];
//设置边框样式
textField.borderStyle = UITextBorderStyleNone;
//当输入框没有内容时,默认文字
textField.placeholder = @"请填写";
//输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//每输入一个字符就变成点 用语密码输入
//textField.secureTextEntry = YES;
//再次编辑就清空
textField.clearsOnBeginEditing = YES;
//内容对齐方式
textField.textAlignment = NSTextAlignmentLeft;
//设置键盘的样式
textField.keyboardType = UIKeyboardTypeDefault;
//return键变成什么键
textField.returnKeyType =UIReturnKeySearch;
//设置字体大小
textField.font = [UIFont systemFontOfSize:14];
//设置背景颜色
textField.backgroundColor = [UIColor redColor];
//设置代理,这个是关键
textField.delegate = self;
textField ;
}) ;
}
return _searchTF ;
}
关键的地方是要设置一下属性:
把returnKeyType设置为UIReturnKeySearch
2、设置代理
@interface HBChangeMentorVC ()<UITextFieldDelegate>
3、实现方法
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[self.searchTF resignFirstResponder];
//搜索操作
return YES;
}
END.