我想知道如何使键盘消失时,用户触摸以外的UITextField。


当前回答

在我的新发展中,我以巴里为例。它工作得很好!但我不得不包括一个轻微的变化,要求解散键盘,只有文本字段被编辑。

所以,我在Barry的例子中加入了以下内容:

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
    _textBeingEdited = textField;
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
    _textBeingEdited = nil;
}

同时,我修改了hideKeyboard方法如下:

- (IBAction)hideKeyboard:(id)sender
{
    // Just call resignFirstResponder on all UITextFields and UITextViews in this VC
    // Why? Because it works and checking which one was last active gets messy.
    //UITextField * tf = (UITextField *) sender;
    [_textBeingEdited resignFirstResponder];
}

其他回答

发送消息resignFirstResponder的文本文件,把它放在那里。请参阅这篇文章了解更多信息。

这是一个很好的通用解决方案:

objective - c:

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

迅速:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    self.view.endEditing(true)
}

基于@icodebuster解决方案:https://stackoverflow.com/a/18756253/417652

有一件事很重要……

添加手势识别到视图可以覆盖它的子视图。 所以只执行一个动作。 如果你想通过父视图和子视图执行多重识别操作… 你必须使用

 tap.cancelsTouchesInView = false

完整代码如下:

 let tap = UITapGestureRecognizer(target: self, action: #selector(self.superViewClicked))
        tap.numberOfTapsRequired = 1
        tap.delegate = self
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)




  @objc func superViewClicked(){
        
        self.searchTF.resignFirstResponder()
    }

在外面点击关闭软键盘的最佳解决方案是什么?

我将解释一个条件:

想想看:你在顶部有UISearchBar, 然后添加头表视图!

解决方法很简单:

跟踪软键盘打开或关闭 添加手势识别头视图 实现表视图didSelectRowAt的委托

完整的代码:

   var isSoftKeyboardActive = false

    override func viewDidLoad() {
    
    
        var tap = UITapGestureRecognizer(target: self, action: #selector(self.tableHeaderClick))
            tap.numberOfTapsRequired = 1
            tap.delegate = self
            tap.cancelsTouchesInView = false
            self.headerOfTableView.addGestureRecognizer(tap)
    
    }
    
          override func viewWillAppear(_ animated: Bool) {
                  super.viewWillAppear(animated)
                 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: UIResponder.keyboardWillHideNotification, object: nil)
                  NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: UIResponder.keyboardWillShowNotification, object: nil)
              }
              
              override func viewWillDisappear(_ animated: Bool) {
                  super.viewWillDisappear(animated)
                  NotificationCenter.default.removeObserver(self)
              }
        


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          if isSoftKeyboardActive == true{
               
               self.searchTF.resignFirstResponder()
           }else{
           
               selectedRow = indexPath.row
               performSegue(withIdentifier: "segueWebview", sender: self)
               
           }
    }
    


 
    @objc func tableHeaderClick(){
         
        self.searchTF.resignFirstResponder()
    
     }
    
    
    
  @objc func keyboardWillAppear() {

        
        isSoftKeyboardActive = true
    }

    @objc func keyboardWillDisappear() {
        
        isSoftKeyboardActive = false
        
    }
 

它是如何工作的:

如果软键盘是打开的,用户点击头软键盘关闭 如果软键盘打开,用户点击单元格,键盘关闭,不执行单元格点击

你可以使用UITapGestureRecongnizer方法通过点击UITextField之外的按钮来取消键盘。通过使用这个方法,每当用户在UITextField之外点击时,键盘就会被驳回。下面是使用它的代码片段。

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissk)];

    [self.view addGestureRecognizer:tap];


//Method
- (void) dismissk
{
    [abctextfield resignFirstResponder];
    [deftextfield resignFirstResponder];

}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first{
     view.endEditing(true)

     }
}