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