Search code examples
swiftxcode10touchesbegan

textFieldShouldReturn func not working on scrollView


How could i make the text disappeared when the user tap the screen when UIscrollview is active

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

I'm using the typical func for get and dismiss the keyboard

I try to

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

Solution

  • in ViewDidLoad

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

    elskroll must be your UIScrollView name:

    outside the ViewDidLoad

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