在iOS里默认键盘弹出后用户点击其他地方是不会让键盘回收的
用户体验非常不好
解决办法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.fieldPhone resignFirstResponder];
}
这种解决方案不会给予用户提示,只能让用户去臆测
在体验了众多App 中发现有款App在键盘的右上角加入了一个Button用以隐藏键盘
(具体是哪个App忘记了)

实现代码
CGFloat width = 90 ;
CGFloat height = 36 ;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height)];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width- width, 0, width, height)];
[btn setTitle:@"隐藏键盘" forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:16];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[view addSubview:btn];
[btn setBackgroundColor:[UIColor whiteColor]];
[view setBackgroundColor:[UIColor clearColor]];
[btn addTarget:self action:@selector(resignFirstResponder) forControlEvents:UIControlEventTouchUpInside];
self.inputAccessoryView = view;
UITextField中的inputView如果不为nil的话不会弹出系统键盘
inputAccessoryView不为nil则会跟着系统键盘一起弹出
网友评论