在iOS里默認(rèn)鍵盤彈出后用戶點(diǎn)擊其他地方是不會讓鍵盤回收的
用戶體驗非常不好
解決辦法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.fieldPhone resignFirstResponder];
}
這種解決方案不會給予用戶提示钦听,只能讓用戶去臆測
在體驗了眾多App 中發(fā)現(xiàn)有款A(yù)pp在鍵盤的右上角加入了一個Button用以隱藏鍵盤
(具體是哪個App忘記了)
實現(xiàn)代碼
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的話不會彈出系統(tǒng)鍵盤
inputAccessoryView不為nil則會跟著系統(tǒng)鍵盤一起彈出