美文网首页
iOS收起键盘

iOS收起键盘

作者: 翱翔的魁魁 | 来源:发表于2016-09-07 10:16 被阅读69次

    在开发过程中,为了实现点击屏幕其它位置收起键盘的目的,我们使用过许多的方法。如果是在UIViewController中收起键盘,除了通过调用控件的resignFirstResponder方法外,还有其它的方法。

    第一种方法

    重载
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    方法,然后在此方法中执行[self.view endEditing:YES]
    代码示例如下:

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
           [self.view endEditing:YES];
    }
    

    如果获取当前UIViewControll比较困难时,可以采用第二种或者第三种方法。直接执行以下两个方法中的一个即可达到效果。

    第二种方法
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
    第三种方法
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    

    相关文章

      网友评论

          本文标题:iOS收起键盘

          本文链接:https://www.haomeiwen.com/subject/lbodettx.html