给view加一个点击事件,点击时收起键盘:

    override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        self.view.endEditing(true)
    }

 

open var cancelsTouchesInView: Bool // default is YES. causes touchesCancelled:withEvent: or pressesCancelled:withEvent: to be sent to the view for all touches or presses recognized as part of this gesture immediately before the action method is called.

如果不加tap.cancelsTouchesInView = false这一句,view中其他的点击事件会失效,例如tableview的didSelectRow。

相关文章:

  • 2022-12-23
  • 2021-08-04
  • 2021-07-05
  • 2022-12-23
  • 2021-06-29
  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-28
  • 2021-09-27
  • 2021-09-05
  • 2021-12-05
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
相关资源
相似解决方案