简体   繁体   中英

iOS: UIView losing tap recognition with UITabGestureRecognizer enabled

I am using an imported button object, Floaty ( https://github.com/kciter/Floaty ) which is a subclass of the UIView class. In one of the view controllers where I am using this button (which includes a Text Field), the button fails to be selected when tapped if I include the following line to close the keyboard when tapped away from...

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))
self.view.addGestureRecognizer(tapGesture)

The idea for how to close the keyboard came from this SO query: iOS - Dismiss keyboard when touching outside of UITextField

I thought part of the issue could be that the Floaty button needs to be in the front, so I added the following line of code, found from this SO query: Losing tap recognition after adding a UIScrollView under a UIButton

floaty.bringSubviewToFront(self.view)

I added this line to the end of my ViewDidLoad() function in the view where it is used.

(Important maybe to note is that all of the aforementioned code is in the ViewDidLoad function for my view controller)

Is there another way to handle closing a keyboard when tapping away from a text field? Or is there something else that I should do to the Floaty (UIView) object to handle tap / touch events?

You can try use Hit Testing. With hit testing, you can assign a view that handles touch. This great article explains how to apply hit testing http://smnh.me/hit-testing-in-ios/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM