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


当前回答

看看这个,这是最简单的方法,

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
      [self.view endEditing:YES];// this will do the trick
}

Or

这个库将处理包括滚动条自动滚动,点击空间隐藏键盘等…

https://github.com/michaeltyson/TPKeyboardAvoiding

其他回答

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

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

    [self.view addGestureRecognizer:tap];


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

}

这一定是通过触摸外部隐藏键盘的最简单方法:

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

(从如何解散键盘时,用户点击其他区域以外的文本框?)

在viewDidLoad 斯威夫特4.2 代码是这样的

let viewTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(dismissKeyboard))
        view.addGestureRecognizer(viewTap)
@objc func dismissKeyboard() {
        view.endEditing(true)
    }

有一件事很重要……

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

 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
        
    }
 

它是如何工作的:

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

@Jensen2k回答的Swift版本:

let gestureRecognizer : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(gestureRecognizer)

func dismissKeyboard() {
    aTextField.resignFirstResponder()
}

一个衬套

self.view.addTapGesture(UITapGestureRecognizer.init(target: self, action: "endEditing:"))