textFieldShouldReturn 函数不适用于 scrollView

textFieldShouldReturn func not working on scrollView

当 UIscrollview 处于活动状态时,如何让用户点击屏幕时文本消失

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

我正在使用典型的函数来获取和关闭键盘

我尝试

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        UIScrollView.endEditing(true)
    }

这个在viewdidLoad()

let scrollViewTap = UITapGestureRecognizer(target: self, action: #selector(self.scrollViewTapped))
        scrollViewTap.numberOfTapsRequired = 1
        scroll_view.addGestureRecognizer(scrollViewTap)

和 class

中的这个
@objc func scrollViewTapped() {
        scroll_view.endEditing(true) 
        self.view.endEditing(true) // anyone
    }

在 ViewDidLoad 中

let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.touch))
        recognizer.numberOfTapsRequired = 1
        recognizer.numberOfTouchesRequired = 1
        elskrolll.addGestureRecognizer(recognizer)

elskroll 必须是您的 UIScrollView 名称:

在 ViewDidLoad 之外

@objc func touch() {
        //print("Touches")
        self.view.endEditing(true)
    }