收起键盘的多种方法

来源:互联网 发布:兼职数据录入是真的吗 编辑:程序博客网 时间:2024/06/06 03:10

在UIVIewController中收起键盘,我们一般调用相应控件的 resignFirstResponder 方法,除此之外,还有三种办法:

1. 重载 UIViewController 中的 touchesBegan 方法,然后在该方法里调用 endEditing: 方法,这样点击UIViewController 的任意地方,都可将键盘收起

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.viewendEditing:YES];

}


2. 直接执行 

[[UIApplicationsharedApplication] sendAction:@selector(resignFirstResponder)to:nilfrom:nilforEvent:nil] 方法。用于在获得当前 UIViewController 比较困难的时。

   

3.直接执行

 [[[UIApplicationsharedApplication] keyWindow] endEditing:YES] 方法。

0 0
原创粉丝点击