textFieldShouldReturn func无法在scrollView上运行

问题描述 投票:0回答:2

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

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

我正在使用典型的func来获取和解除键盘

我试着

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        UIScrollView.endEditing(true)
    }
swift xcode10 touchesbegan
2个回答
0
投票

这在viewdidLoad()中

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

这在课堂上

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

0
投票

在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)
    }
© www.soinside.com 2019 - 2024. All rights reserved.