iOS 隐藏键盘的几种常见方法

1.设置return key,然后为Did End On Exit事件添加响应方法,并在方法内添加代码:[self.textfieldName resignFirstResponder]。

2.将背景view设置为UIControl类,然后为Touch Down事件添加响应方法,并在方法内添加代码:[self.textfieldName resignFirstResponder]。

3.重载- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event方法,并在方法内添加代码:[self.textfieldName resignFirstResponder] 或者 [self.view endEditing:YES]

4.直接执行[[UIApplication sharedApplication]sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; 用于在获得当前UIViewController比较困难的时候。

5.直接执行[[[UIApplication sharedApplication]keyWindow]endEditing:YES]。

原文地址:https://www.cnblogs.com/ficow/p/5223314.html