美文网首页
收回键盘几种方式

收回键盘几种方式

作者: 奋斗的蜗牛 | 来源:发表于2016-06-21 15:50 被阅读93次
    1. 点击键盘的return键收回键盘
    //需要实现UITextFieldDelegate协议中的方法  
    -(BOOL)textFieldShouldReturn:(UITextField *)textField  
    {  
        [textField resignFirstResponder];  
        return YES;  
    }  
    
    • 点击屏幕空白处收回键盘
     - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [self.view endEditing:YES];
    }
    

    手势现实

    - (void)viewDidLoad
    {    
       self.view.userInteractionEnabled = YES;   
       UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];    
       [self.view addGestureRecognizer:singleTap];
       
    }
    - (void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer
    {   
       [self.view endEditing:YES];  
    }
    
    • 拖拽收回键盘
     - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        [self.view endEditing:YES];
    } 
    

    相关文章

      网友评论

          本文标题:收回键盘几种方式

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