键盘在模式下,可能没有返回或者是完成按钮,因此有的时候需要人为的添加一个返回或者是完成按钮。
首先,创建一个工程:
@interface ViewController ()<UITextFieldDelegate>
{
UITextField *testField;
}
@end
创建一个全局的,方便调用
在自己想要调用的方法中创建textField
- (void)viewDidLoad {
[super viewDidLoad];
testField =[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
[self.view addSubview:testField];
testField.placeholder = @"搜索内容";
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[topView setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(2, 5, 50, 25);
[btn addTarget:self action:@selector(dismissKeyBoard) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@"完成" forState:UIControlStateNormal];
// [btn setImage:[UIImage p_w_picpathNamed:@"shouqi"] forState:UIControlStateNormal];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];
NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneBtn,nil];
[topView setItems:buttonsArray];
[testField setInputAccessoryView:topView];
}
-(void)dismissKeyBoard
{
[testField resignFirstResponder];
}
这样一个简单的键盘回收demo就完成的