I have a UIScrollView on which I place some text fields and a tableView.
This is not a table delegate issue. I can get didSelectRowAt if I click and drag on a row. Just the simple row tap does not get through.
I do not show the table implementation below because it generally works and I think it is the gesture bit that is causing the problem.
I have added a gesture recognizer to the UIScrollView so that if you tap on the view background you will end the editing of the current field. This works to dismiss editing and keyboard, but for a UITableView I do not get the didSelectRowAt when I tap on a row. I have to press the row and drag horizontally in order to get the didSelectRowAt method call.
Is there a way to forward the tap gesture to the UITableView? Or is it just a bad idea to use the gesture recognizer on the UIScrollView if I have a table on that view? A button on that same view will get the tap and process the button event. Not sure why the table will not get the tap. The table will scroll also properly, but does not see the row tap.
The main UIView Class Methods
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(white: 1, alpha: 1.0) // was .97
// Container
setupScrollView()
registerKeyboardNotifications()
addGestureRecognizer()
}
func setupScrollView() {
view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.contentSize = CGSize(width: view.frame.width, height: 1000)
scrollView.autoresizingMask = UIViewAutoresizing.flexibleBottomMargin
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0).isActive = true
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor, constant: 0).isActive = true
}
func addGestureRecognizer() {
// When tap on the container -- end editing on current field
let tapRecognizer = UITapGestureRecognizer(target: self,action: #selector(tapDidTouch(sender: )))
self.scrollView.addGestureRecognizer(tapRecognizer)
}
@objc func tapDidTouch(sender: Any) { // this method eats UITableView clicks
self.view.endEditing(true)
}
The extension for handling keyboard
extension TestView {
func registerKeyboardNotifications() {
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self,selector: #selector(keyboardWillShow(notif:)),name: .UIKeyboardWillShow,object: nil)
notificationCenter.addObserver( self, selector: #selector(keyboardWillHide(notif:)),name: .UIKeyboardWillHide,object: nil)
notificationCenter.addObserver(self, selector: #selector(keyboardWillShow(notif:)), name: .UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardWillShow(notif: Notification) {
guard let frame = notif.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return}
scrollView.contentInset = UIEdgeInsets(top: 0.0,
left: 0.0,
bottom: frame.height,
right: 0.0)
}
@objc func keyboardWillHide(notif: Notification) {
scrollView.contentInset = UIEdgeInsets()
}
}