textFieldShouldReturn函数在scrollView上不起作用

时间:2018-10-25 16:47:34

标签: swift xcode10 touchesbegan

当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)
    }

2 个答案:

答案 0 :(得分:0)

viewdidLoad()中的此

    int pageKey = 0;
    int count = 1;

    for(int i = 1; i <= 200; i++) {
        count ++;
        if(i % 50 == 0) {
            pageKey += 1;
            count = 1;
        }
    }

和班上的

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

答案 1 :(得分: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)
    }