类方法创建一个带收回键盘的TextField
默认键盘有时不带键盘回收按键,添加一个Button按下给键盘发送收回消息
+ (UITextField *)createTextField:(CGRect)frame {
UITextField *textField = [[UITextField alloc] initWithFrame:frame];
textField.backgroundColor = [UIColor clearColor];
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.textColor = [UIColor blackColor];
textField.font = FONT15;
//创建ToolBar作为TextField的AccessoryView
UIToolbar *toolBar = [[UIToolbar alloc]init];
[toolBar setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
//创建收起键盘按键
UIButton *returnBtn = [[UIButton alloc]init];
[returnBtn setFrame:CGRectMake(ScreenW - 100, 0, 100, 44)];
[returnBtn setTitle:@"确定" forState:UIControlStateNormal];
[returnBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[returnBtn.titleLabel setFont:FONT15];
//点击对textField发送收起键盘消息
[returnBtn addTarget:textField action:@selector(resignFirstResponder) forControlEvents:UIControlEventTouchUpInside];
//添加按键到ToolBar上
[toolBar addSubview:returnBtn];
//添加ToolBar到TextField上
[textField setInputAccessoryView:toolBar];
return textField;
}