iOS隐藏键盘的几种方法

以前总是遇到这类问题,没怎么在意,但是时间一长又容易忘掉,索性总结一下,全部记录下!

首先说明两种可以让键盘隐藏的方法:

1、[view endEditing:YES]  这个方法可以让整个view取消第一响应者,从而让所有控件的键盘隐藏,所用控件必须作用在当前的view上。

2、[textFiled resignFirstResponder] 这个则是比较常用的让某个textFiled的键盘隐藏。


接下来就是几种实现方式:

第一种: 使用view的touchesBegan:触摸事件来实现对键盘的隐藏,当点击view的区域就会触发这个事件

  1. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

  2.     if(![textField isExclusiveTouch]) {
  3.     [textFiled resignFirstResponder]; 
  4.     }
  5. }  

第二种:创建自定义的触摸手势来实现对键盘的隐藏:

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];  
  5.     // 设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。  
  6.     tapGestureRecognizer.cancelsTouchesInView = NO;  
  7.     // 将触摸事件添加到当前view上 
  8.     [self.view addGestureRecognizer:tapGestureRecognizer];  
  9. }  
  10.   
  11. -(void)keyboardHide:(UITapGestureRecognizer*)tap
  12. {  
  13.     [textFiled resignFirstResponder];  
  14. }  


第三种:修改xib中UIView的Custom class为UIControl,UIControl是一些常用控件如UIButton的父类,是UIView的派生类,实现了对触摸和下按的封装。

1、首先设置xib中得UIView的Custom class为UIControl


2、设置关系事件,将xib中得UIView拖到.h区中


注意要设置好事件为Touch Up Inside。

3、编写隐藏代码:


  1. - (IBAction)touchView:(id)sender
  2.  {  
  3.      [self.view endEditing:YES];  
  4. }  

4、利用通知方法

- (void)setUpForDismissKeyboard
{
    NSNotificationCenter *noti = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [noti addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view addGestureRecognizer:singleTap];
                }];
    [noti addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view removeGestureRecognizer:singleTap];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer
{
    // 此Method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
}


参考资料:         http://blog.csdn.net/swingpyzf/article/details/17091567

                  http://www.cnblogs.com/leipei2352/p/3589506.html

                  http://blog.csdn.net/johnson_gs/article/details/7484069

                  http://justcoding.iteye.com/blog/1476171

                  http://blog.csdn.net/lianbaixue/article/details/11108593

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值