1、点击Return按扭时收起键盘。注意:需要遵从UITextFieldDelegate协议,并设置好代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField { return [textField resignFirstResponder]; }
或者
- (BOOL)textFieldShouldReturn:(UITextField *)textField { return [textField endEditing:YES]; }
或者(这里的View必须是继承于UIControl)
- (BOOL)textFieldShouldReturn:(UITextField *)textField { return [self.view endEditing:YES]; }
或者
-(BOOL)textFieldShouldReturn:(UITextField *)textField { return [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; }
2、点击背景View收起键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //以上4种收起方式中的任何一种 }
3、通用的收起键盘方式
上面第4种收起键盘方式,可以用在任何地方的代码
原文:http://my.oschina.net/u/2392723/blog/513810